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;
}
标准 2003 14.3.2/5 说:
for a non-type template-parameter of type pointer to object, qualification conversions (4.4) and the array-to-pointer conversion (4.2) are applied. [Note: In particular, neither the null pointer conversion (4.10) nor the derived-to-base conversion (4.10) are applied. Although 0 is a valid template-argument for a non-type template-parameter of integral type, it is not a valid template-argument for a non-type template-parameter of pointer type. ]
为什么要施加这样的限制?
最佳答案
2 个原因恕我直言:
地址直到链接时才为人所知。那是在做出任何模板扩展决定之后。事实上,在与位置无关的代码中,地址直到运行时才为人所知。
(type *)0 和 int(0) 之间长期以来存在歧义。 c++11 使用 nullptr_t 类的 nullptr 值解决了这个问题。
关于c++ - 派生为模板参数的基本转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27625392/