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?
#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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.