Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Derived class type in template argument doesn't compile

Tags:

c++

templates

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

like image 570
user6435212 Avatar asked Apr 27 '11 12:04

user6435212


1 Answers

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

like image 82
Prasoon Saurav Avatar answered Oct 22 '22 21:10

Prasoon Saurav