In 6.8.3 of the C++11 standard it says:
If, during parsing, a name in a template parameter is bound differently than it would be bound during a trial parse, the program is ill-formed.
What is an example of a program that is ill-formed as a result of this requirement?
For example, given a specialization Stack<int>, “int” is a template argument. Instantiation: This is when the compiler generates a regular class, method, or function by substituting each of the template's parameters with a concrete type.
In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.
8. Why we use :: template-template parameter? Explanation: It is used to adapt a policy into binary ones.
A template non-type parameter is a template parameter where the type of the parameter is predefined and is substituted for a constexpr value passed in as an argument. A non-type parameter can be any of the following types: An integral type. An enumeration type. A pointer or reference to a class object.
#include <iostream> #include <typeinfo> typedef const int cint; template <int a> struct x { static cint b = 0; }; template <> struct x<42> { typedef cint b; }; cint w = 17; int main () { cint (w)(42), (z)(x<w>::b); std::cout << typeid(z).name() << std::endl; }
The first declaration in main()
needs to be disambiguated, so a trial parse is performed. During this parse, the local w
is unknown, since the parse is purely syntactic (things are only parsed, no semantic actions are performed). Consequently, w
is a global constant, its value is 17, x<w>::b
is a value, and z
is a variable.
During the real parse, semantic actions take place. Thus the name w
is bound to the freshly declared local constant, its value is 42, x<w>::b
becomes a type, and z
is a function declaration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With