Dan*_*ins 5 c++ operator-overloading c++14
给定具有单个模板参数 T 的模板类 A,是否可以仅重载 A 中可用于类型 T 的运算符?例如:
template <typename T>
class A
{
public:
#if hasOperator(T, +=)
T& operator +=(const T &rhs)
{
mValue += rhs;
return mValue;
}
#endif
private:
T mValue;
}
int main()
{
A<int> a;
a += 8; //+= will forward to the += for the int
struct Test { /*no operators defined*/ };
A<Test> b; //+= is not implemented since Test does not implement +=
}
Run Code Online (Sandbox Code Playgroud)
我正在编写一个通用包装类,它的行为需要与模板类型完全一样。因此,如果 T 具有运算符 +=,则 A 将(在编译时)相应地重载 +=。是的,我可以继续在 A 中实现每个运算符,但是当 T 没有某个运算符时编译器会出错。起初我认为模板专业化可能是答案,但这需要对每种类型进行专业化。虽然这可以工作并且需要大量输入,但它不会,因为 A 需要使用任何类型(不仅仅是专门的类型)。
使用表达式 SFINAE 将您的operator+重载决策集中删除,除非T定义operator+
template <typename T>
class A
{
private:
T mValue;
public:
template<typename U=T>
auto operator +=(const U &rhs)
-> decltype(mValue += rhs)
{
mValue += rhs;
return mValue;
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
717 次 |
| 最近记录: |