简体   繁体   中英

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM