gpt4 book ai didi

c++ - 默认函数可以自动返回吗?

转载 作者:行者123 更新时间:2023-12-01 08:55:25 26 4
gpt4 key购买 nike

考虑以下类别:

class example
{
public:
auto & operator =(const example &) = default;
auto & operator =(example &&) = default;
};
这些声明是否合法?

最佳答案

auto & operator =(const example &) = default;
auto & operator =(example &&) = default;

Are those declarations considered legal?


没有。

[dcl.spec.auto] ... If the declared return type of the function contains a placeholder type, the return type of the function is deduced from non-discarded return statements, if any, in the body of the function ([stmt.if]).


默认函数定义没有主体,该主体与引用的规则冲突。没有什么可以推断出返回类型的,也没有规则说明这种情况下返回的类型。 operator<=>有一个特殊的规则,指定使用 auto时返回的类型是什么,如 Caleth's answer所示,但 operator=没有这样的规则。我认为没有理由不能引入这样的规则以允许自动使用默认赋值运算符。

关于c++ - 默认函数可以自动返回吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62565354/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com