重载运算符返回类型推导失败

发布于 2025-01-14 21:36:36 字数 1532 浏览 0 评论 0原文

因此,我有一个带有 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 values 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南渊 2025-01-21 21:36:36

正如 @NathanPierson 提到的:

Number 指的是运算符内的 Number 。不幸的是,您不能在这里使用模板参数推导。

不过,返回类型可以根据附加组件的类型来确定。您可以使用尾随返回类型来指定返回类型。这假设您在从函数返回之前不需要使用结果。

class Number
{
public:
    ...

    template<typename U>
    auto operator+(Number<U> other) -> Number<decltype(other.value + value)>
    {
        return {value + other.value};
    }

};

您还可以在命名空间范围内实现运算符:

template<class T, class U>
auto operator+(Number<T> const& s1, Number<U> const& s2)
{
    auto val = s1.value + s2.value;
    Number result(val);
    return result;
}

As @NathanPierson mentioned:

Number refers to Number<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.

class Number
{
public:
    ...

    template<typename U>
    auto operator+(Number<U> other) -> Number<decltype(other.value + value)>
    {
        return {value + other.value};
    }

};

.

You could also implement the operator in namespace scope:

template<class T, class U>
auto operator+(Number<T> const& s1, Number<U> const& s2)
{
    auto val = s1.value + s2.value;
    Number result(val);
    return result;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文