开发者

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*?


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]

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. [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. ]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜