Converting the base for the template argument
struct CL1{};
struct CL2:CL1{};
template<CL1*>
struct TMPL{};
CL2 cl2;
int main()
{
TMPL<&cl2> tmpl; //error: could not convert template argument ‘& cl2’ to ‘CL1*’
return 0;
}
The 2003 14.3.2 / 5 standard says:
for an atypical template-parameter of a type pointer to an object, qualification conversions (4.4) and conversion of an array into a pointer (4.2). [Note: in particular, neither the null pointer conversion (4.10) , but the conversion from derivative to base (4.10) is attached . Although 0 is a valid template argument for a non-template type integral type parameter, it is not valid template-argument for a type template of a pointer type. ]
Why do such restrictions apply?
source to share
2 reasons IMHO:
-
The addresses are not known until the time of the link. This is good after making any decisions to extend the templates. Indeed, in position-independent code, the addresses are not known until runtime.
-
There is a long-standing ambiguity between (type *) 0 and int (0). C ++ 11 heals this with the nullptr value of the nullptr_t class.
source to share