Document number: P1324R1
Reply-To: Mihail Naydenov <mihailnajdenov at gmail dot com>
Initial release, not presented.
This proposal is direct continuation (a “reply”) to Yet another approach for constrained declarations (P1141).
It proposes to go “one step further” and allow specifying a type in the place of
auto in all constrained declarations, suggested by P1141. In other words
void sort(Sortable auto& c); becomes
void sort(Sortable S& c); and as a result the specified typename
S is introduced for the current function.
This proposal is a pure extension of P1141.
In a nutshell, allow
to also be written as:
S will be equivalent to a call to
decltype, given the original P1141 declaration, or using the standard syntax:
The main motivation is explored in many other papers, like for example Concepts in-place syntax (P0745R0).
It basically boils down to the fact, it is often just too convenient to have the type be known as it makes the already verbose generic code slightly less so:
Also, when we need to constrain multiple arguments, terse form becomes impractical
And sometimes it is even impossible to create a declaration using the terse form alone:
Consider what an
int n states, reading it right to left.
“An object named ‘n’ of type integer.”
But what is a type here? A type is representation in memory - technically - but a constraint semantically. In the case here the value can only be in certain range and only whole numbers. One can say, the value of a variable is constrained by its type.
In a way, we have been writing constrains for decades, constrains on values. Let’s rephrase the above with that in mind:
“Object named ‘n’, that can have [only] values, belonging to the set of the ‘int’ type.”
Ok, given the above, why should constrains on types be expressed any different?
Number Num n:
“Object named ‘n’, that can have [only] values, belonging to the set named ‘Num’, ‘Num’ can [only] be a type, belonging to the set of the ‘Number’ concept.”
As You can see, we are not inventing anything, we are just_ recursively applying a constraint,_ right to left, as we always have!
In the case of constrained
auto, we are simply omitting the name, effectively stating, we don’t needed it -
Number auto n.
Sidenote, there are proposals, advertising the possibility to be able to omit the variable name also. Using the syntax from P1110R0, we can have fully unnamed, fully unconstrained declaration -
auto __- alongside the fully named, fully constrained declaration we just described, or any combination b/w named/unnamed variable, named/unnamed type, constrained/unconstrained type and constrained/unconstrained variable.
It must be pointed out,
auto already stands-in for an omitted type! At the moment in all, but one case (structured bindings), if we replace
auto with a type we end up with a valid declaration, the meaning might change, but the declaration will be valid. This means the user already associates
auto with a “type stand-in”, even if the declaration meaning changes by the substituting one with the other.
There is no reason not to continue this relationship - if the user sees an
auto, he should be able to write a type name in its place. What this name represents will be new (a name introduction), but the context is also new; besides we don’t stride too much away from the established usage.
auto copy(InputIterator auto begin, decltype(begin) end, OutputIterator auto out) -> decltype(out); template<InputIterator It, OutputIterator OIt> copy(It begin, It end, OIt out) -> OIt; auto copy(InputIterator It begin, It end, OutputIterator OIt out) -> OIt; //< **proposed** // Even compared to the original, concepts-instead-of-types syntax! auto copy(InputIterator being, decltype(begin) end, OutputIterator out) -> decltype(out);
Compared to other contemporary languages, this syntax holds its ground. This is because we have zero extra syntactical noise.
C++ has long been considered verbose, when it comes to metaprogramming. Now the tables have turned and it is actually the cleanest.
In fact the cleanest possible.
One can argue, if we had concepts-instead-of-types, then we would have “the cleanest possible” form. This is true as long as there are no extra constrains that need to be applied. In that case, a
decltypewould be needed and the results will not be as pretty.
Type and variable declaration have always been in C++, inherited from C. And they still have their uses.
As shown, two-in-one declaration is nothing new and although the above expressions can be written separately, this does not change the fact, an extremely similar syntax, with de facto the same meaning (introduce a typename inline) is already here - we just reinvent it for the new era.
In contrast to the elaborate type specifier the declaration is limited to the current function declaration, as-if the type was declared as template parameter!
By this point it should be evident, enabling type name introduction in the proposed way, not only does not introduce new constructs and concepts, but serve to reinforce established ones.
In order to prevent confusion in the case, parameter names are omitted from a function declaration, proposed is to always treat function signatures, with two identifiers for a parameter, as the well known typename + param name pair.
Number is a concept, this will fail to compile.
The user will have to use a different syntax:
With the above limitation, generic functions will remain recognizable at a glance, even when using the terse syntax:
Having return type name introduction, combined with trailing return type could enable us to have an interesting usage:
Current alternatives are all inferior:
Allowing concepts to introduce the type they constrain will radically change the the way we write generic code. Doing it so with effectively zero new constructs, reusing established practices, will ease the adoption of the terse form as the preferred one, possibly even for unconstrained cases.