重载运算符返回类型推导失败
因此,我有一个带有 T
类型的单个属性 value
的类模板,我尝试重载 +
运算符,添加两个此对象class 会生成该类的另一个对象,其中 value
是两者的 value
之和。这是代码:
template<typename T>
class Number
{
public:
//constructors
Number(T value)
: value(value) {}
//attributes
const T value;
//operators
//addition +
template<typename U>
auto operator+(Number<U> other)
{
auto val = this->value + other.value;
Number output(val); //instantiating object of class Number, using constructor
std::cout << "output value: "<<output.value << std::endl;
std::cout << "auto val: "<< val << std::endl;
return output;
}
};
int main()
{
Number<int> x(2);
Number<float> y(2.5);
Number z=x+y;
std::cout << "x = " << x.value << std::endl;
std::cout << "y = " << y.value << std::endl;
std::cout << "x+y = " << z.value << std::endl;
return 0;
}
。 输出是:
output value:4
auto val: 4.5
x = 2
y = 2.5
x+y = 4
。 显然,存在从 float 到 int 的不需要的类型转换,我试图找出如何避免这种情况而不求助于专门化。运算符输出类型的显式声明无法编译。我知道这里的失败在于 auto
类型输出变成了 Number
类型,在本例中是 Number
> 但即使在运算符定义范围 { }
内实例化新对象时,它也必须是 int
吗? 我希望在这个简单的练习中得到一些帮助,提前谢谢您!
So I have this class template with a single attribute value
of type T
where I try to overload the +
operator, s.t. adding two objects of this class results in another object of this class where the value
is the sum of the value
s of the two. Here's the code:
template<typename T>
class Number
{
public:
//constructors
Number(T value)
: value(value) {}
//attributes
const T value;
//operators
//addition +
template<typename U>
auto operator+(Number<U> other)
{
auto val = this->value + other.value;
Number output(val); //instantiating object of class Number, using constructor
std::cout << "output value: "<<output.value << std::endl;
std::cout << "auto val: "<< val << std::endl;
return output;
}
};
int main()
{
Number<int> x(2);
Number<float> y(2.5);
Number z=x+y;
std::cout << "x = " << x.value << std::endl;
std::cout << "y = " << y.value << std::endl;
std::cout << "x+y = " << z.value << std::endl;
return 0;
}
.
The output is:
output value:4
auto val: 4.5
x = 2
y = 2.5
x+y = 4
.
Obviously there occurs and undesired type conversion, from float
to int
and I am trying to find out how to avoid this without resorting to specialization. Explicit declaration of the operator output type does not compile. I understand that here the failure lies in that the auto
type output becomes the type Number<T>
, which in this case is Number<int>
but does it have to be int
even when a new object is instantiated within the operator definition scope { }
?
I'd appreciate some help in this simple exercise, thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 @NathanPierson 提到的:
Number
指的是运算符内的Number
。不幸的是,您不能在这里使用模板参数推导。不过,返回类型可以根据附加组件的类型来确定。您可以使用尾随返回类型来指定返回类型。这假设您在从函数返回之前不需要使用结果。
。
您还可以在命名空间范围内实现运算符:
As @NathanPierson mentioned:
Number
refers toNumber<T>
inside the operator. You unfortunately cannot use template argument deduction here.The return type can be determined based on the type of the additon though. You could use a trailing return type to specify the return type. This assumes you don't need to use the result before returning from the function.
.
You could also implement the operator in namespace scope: