ORNL / ORNL/cpp-proposals-pub

Use global-scope traits instead of nested type aliases for mdspan concepts

Open
#12 3 comments 0 reactions 1 assignee View on GitHub

@brycelelbach is already working on this.

Since May 1, 2018.

discussion subject - mdspan (P0009)
Dominant language
HTML
Stars
29
Forks
26
PR merge metrics
No merged PRs in 30d

Description

Requiring the definition of nested type aliases in a concept is annoying, because you can't provide a "default" definition, so type authors implementing your concept must define the nested type aliases. This pattern exists mostly for historical reasons:

  • Pre C++11, these nested type aliases were often an unfortunate necessity (Boost/TR1 result_of and (unary|binary)_function, etc). Post C++11, this is frequently syntax sugar as these types can be discovered via decltype, etc.
  • Pre C++11, using separate traits class customization points to define these types was unwieldy because we didn't have type aliases to save us from typename (e.g. typename X<T>::type instead of X_t<T>).

Iterators, containers, and allocators all suffer from this problem.

In modern C++, there's a better solution: define a traits class for each nested type aliases (not a single traits class for all the types, like iterator_traits or numeric_traits), provide a reasonable default, and allow users to customize it. For example:

template <typename MyConcept>
struct X { using type = /* default */; };

template <typename MyConcept>
using X_t = typename X<T>::type;

The executors proposal uses this pattern (executor_future_t, executor_index_t, etc). We should do the same for mdspan concepts like Accessor and Layout.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.