Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Template w/ Pointers - Could not convert template argument

Tags:

c++

templates

I am trying to implement a template

template <class object_t, long size, object_t nullObject>
class lf_deque
{
  // ...
}

when I try to instantiate this template with an int, it compiles fine, but if I try to instantiate with a pointer i get the error:

could not convert template argument '0' to 'int*'

lf_deque<int,  10, 0> intDeque; // WORKS
lf_deque<int*, 10, 0> ptrDeque; // ERROR

any thoughts or ideas why i would get this inconsistency?

like image 774
Jerunh Avatar asked Apr 23 '26 03:04

Jerunh


1 Answers

In templates when a function/class is resolved with ADL(Argument Dependent Lookup) Function Template Argument Deduction, there is no implicit conversion. Only exactly matching paramters can resolve to instantiate a appropriate template function/class. That is the root cause of the error.

The compiler tells you that it cannot implicitly convert last parameter 0 to int *, Since when you pass first argument as int *, object_t is int * and the compiler expects an int * as the third argument as well. It tells you that 0 is an invalid type as the third argument for the class template.

like image 69
Alok Save Avatar answered Apr 24 '26 20:04

Alok Save



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!