[英]Derived class type in template argument doesn't compile
This is the code snippet I have been hopelessly stuck on.这是我一直无可救药的代码片段。
template <class T, T nt>
class C;
struct base{
int i;
} b;
struct derived : base{} d;
C<base*,&d> obj;
Why this giving error could not convert template argument &d to base*
?为什么这个给出错误
could not convert template argument &d to base*
?
When matching an argument to a parameter that is a pointer/reference, derived to base conversions are not considered even if the conversions are valid in other circumstances.在将参数与作为指针/引用的参数匹配时,即使转换在其他情况下有效,也不会考虑派生到基本转换。
14.3/5 [Standard quote just for reference] 14.3/5 [标准报价仅供参考]
If a non-type template-argument cannot be converted to the type of the corresponding template-parameter then the program is ill-formed.
如果非类型模板参数不能转换为相应模板参数的类型,则该程序格式错误。
....
……
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.
对于指向 object 的类型指针的非类型模板参数,应用限定转换 (4.4) 和数组到指针转换 (4.2)。 [Note: In particular, neither the null pointer conversion (4.10) nor the derived-to-base conversion (4.10) are applied .
[注意:特别是,null 指针转换 (4.10)和派生到基址转换 (4.10) 均未应用。 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.
尽管 0 是整数类型的非类型模板参数的有效模板参数,但它不是指针类型的非类型模板参数的有效模板参数。 ]
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.