The following example is working when I manualy replace T
wirh char *
, but why is not working as it is:
template <typename T>
class A{
public:
A(const T _t) { }
};
int main(){
const char * c = "asdf";
A<char *> a(c);
}
When compiling with gcc, I get this error:
test.cpp: In function 'int main()':
test.cpp:10: error: invalid conversion from 'const char*' to 'char*'
test.cpp:10: error: initializing argument 1 of 'A<T>::A(T) [with T = char*]'
Substituting T
with char*
gives a const
pointer to char, while c
is declared as a pointer to const char
.
A solution would be to take pointers and integral types by value and class types by const reference. If you can, use Boost Call Traits which solves these kinds of problems for you.
I guess it's because your function expects const (char *)
(since T is char *
), i.e. you can't change the address it points to, while const char *
stands for (const char) *
, i.e. you can't change the value on the place the pointer points to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With