Reputation: 7539
In c++17 template <auto>
allows to declare templates with arbitrary type parameters. Partially inspired by this question, it would be useful to have an extension of template <auto>
that captures both type and nontype template parameters, and also allows for a variadic version of it.
Are there plans for such an extension in the next c++20 release? Is there some fundamental problem in having a syntax like template<auto... X>
, with X
any type or nontype template parameter?
Upvotes: 2
Views: 540
Reputation: 5613
I can't see how it would be useful that a template argument could be dynamically either a type or a value? The code statements that use types are very different to those that which use constant values introduced through the template argument.
The only way would be a big "if constexpr" which would make it pointless in my view.
Ok, having looked more closely at the referenced question, I guess there is room there for generically pass-through wrapping the various explicit base template implementations that use different parameter orderings. I still fail to see a huge benefit. The compiler errors when it goes wrong are going to be unfathomable, if nothing else!
I remember being told that overloading and templates were going to rid the world of the unfathomable error messages generated from macros. I have yet to see it!
Upvotes: -1
Reputation: 473447
The big issue with trying to do something like that is grammar. Template parameters state up-front whether they are templates, types, or values, and the most important reason for this is grammatical.
C++ is a context-sensitive grammar. That means that you cannot know, just from a sequence of tokens, what a particular sequence of tokens means. For example, IDENTIFIER LEFT_PAREN RIGHT_PAREN SEMICOLON
. What does that mean?
It could mean to call a function named by IDENTIFIER
with no parameters. It could mean to default initialize a prvalue of a class named by IDENTIFIER
. These are rather different things; you might conceptually see them as similar, but C++'s grammar does not.
Templates are not macros; they're not doing token pasting. There is some understanding that a piece of code in a template is supposed to mean a specific thing. And you can only do that if you at least know what kind of thing a template parameter is.
In order to retain this ability, these "omni template parameters" cannot be utilized until you actually know what they mean. So in order to create such a feature in C++, you would need to:
auto
isn't going to fly, as it already has a specific meaning).if constexpr
block, but pattern matching proposals represent an interesting alternative/additional way to handle them (since they can be expressions as well as statements). And expansion statements represent a possible way to access all of the omni parameters in a parameter pack.Upvotes: 1
Reputation: 303087
Are there plans for such an extension in the next c++20 release?
No.
Is there some fundamental problem in having a syntax like
template<auto... X>
, withX
any type or nontype template parameter?
It would be a totally new concept in the language - having a name refer to either a type or a value in the same place. So it'd come with all sorts of additional questions - and probably additional language features to check if X
is a type or not.
The syntax likely cannot be template <auto... X> struct Y { };
since that syntax already has meaning and means a bunch of values and Y<int>{}
is ill-formed.
There are definitely places where such a thing would be useful though. A proposal would just have to address these issues.
Upvotes: 7