使用 = 的模板运算符专用化
template operator specialization with =
我正在尝试实现此模板运算符专用化
template <class T>
class test
{
public:
T value;
};
template <> test &test::operator=<std::string>(const char *rhs)
{ return *this;}
但是使用 g++,我收到此错误:
错误:在没有参数列表的情况下无效使用模板名称"test" 模板<>测试 &test::运算符=(常量字符 *rhs) ^~~~
似乎您只是将模板参数放在了错误的位置。模板化类需要其范围之外的模板参数,因此在这种情况下,返回类型也需要模板参数。
template<class T>
class test
{
public:
test & operator=(const char * rhs) {
return *this;
}
};
template<> test<std::string> & test<std::string>::operator=(const char * rhs) {
// type goes here ^^^^^^^^^ and here ^^^^^^^^^^
return *this;
}
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么比较运算符如此快速
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 增量运算符与后缀混淆
- 调用专用模板时出错"no matching function for call to [...]"
- 仅为某些模板专用化定义转换运算符:预期类型/预期类型说明符
- 专用运算符<<模板类的模板类的另一个类
- 使用 = 的模板运算符专用化
- 运算符<<重载时无法访问专用成员(指定指针)
- 来自另一个类的模板运算符专用化
- 运算符重载和模板专用化
- 重载运算符'<<'(左移)的显式专用化
- 如果给定模板不是运算符参数类型的专用模板,则禁用运算符重载
- 为什么 std::atomic<integral> 专用化不提供乘法和除法赋值运算符?
- 类具有运算符()时的结构专用化
- 无法专用化函数模板。重载运算符<<
- 无法访问QXmlStreamReader的专用成员(运算符=)
- C++模板专用化:更改运算符()的返回类型