template<typename T> struct SomeClass{
void someFunc(const T& data) const {}
};
void testFunc(const int* a) {
SomeClass<int*> some_class;
some_class.someFunc( a);
}
我制作了一个具有非const类型的模板实例。现在,当调用某个函数时,出现以下错误:
error: invalid conversion from ‘const int*’ to ‘int*’
note: initializing argument 1 of ‘void SomeClass<T>::someFunc(const T&) const [with T = int*]’
因此,基本上我的const T&
被视为普通T&
,const
被忽略。为什么?在这种情况下,如何确保编译器将其视为const T&
?
您需要将定义更改为SomeClass<const int*> some_class;
。 T
来自定义,为int*
,编译器在正确地抱怨。