我喜欢玩自动和decltype,然后我想知道是否可以用auto来做泛型运算符。事实上,由于C++ 14等可以这样做:泛型重载运算符
decltype(auto) add(auto v1, auto v2) {
return v1 + v2;
}
不过,我想与含有像这样的值模板类来试试吧:
template<typename T>
class test {
public:
T value;
test(T val) {
value = val;
}
};
,然后我需要超负荷运营商+像这样一个工作:
template<typename T>
T operator+(test<T> const& t1, test<T> const& t2) {
return t1.value + t2.value;
}
这已经相当不错了。不过,我想要一个可以被多个类使用的通用运算符+。像这些:
decltype(t1.value) operator+(auto const& t1, auto const& t2) {
return t1.value + t2.value;
}
template<typename T>
T operator+(auto const& t1, auto const& t2) {
return t1.value + t2.value;
}
它不会编译。
在C++ 14/17中,是否有一种方法可以使泛型重载运算符能够被许多类使用,如我写的那些类?
PS:在这里为您的测试与gcc7快照编译,但不能与铛这似乎不允许代码自动函数原型: link to compiler explorer code
#include <iostream>
template<typename T>
class test {
public:
T value;
test(T val) {
value = val;
}
};
template<typename T>
T operator+(test<T> const& t1, test<T> const& t2) {
return t1.value + t2.value;
}
decltype(auto) add(auto v1, auto v2) {
return v1 + v2;
}
int main() {
decltype(5) v1 = 5;
decltype(v1) v2 = 3;
test<decltype(v1)> t(v1);
test<decltype(v2)> t2(v2);
return add(t, t2);
}
* *事实上,因为C++ 14 et可以做到这一点:*“不,你不能。 'auto'只能用作* lambdas *中的参数占位符。这是Concepts TS将其扩展到常规功能。而C++ 14不包括这一点。 –