默认函数 return 可以自动吗?
Can a default function return auto?
考虑以下 class:
class example
{
public:
auto & operator =(const example &) = default;
auto & operator =(example &&) = default;
};
这些声明是否合法?
不是语言律师回答
根据我的经验,编译器不接受默认特殊成员函数的 auto
return 类型,因此我认为它们确实是标准不允许的。
我所知道的唯一例外是 C++20 默认的三向比较运算符:
#include <compare>
struct X
{
auto operator<=>(const X&) const = default;
};
在 C++20 中,是1
Let R
be the declared return type of a defaulted three-way
comparison operator function, and let xi
be the elements of the
expanded list of subobjects for an object x
of type C`.
- If
R
is auto
, then let cvi Ri
be the type of the expression xi <=> xi
. The operator function is defined as deleted if that
expression is not usable or if Ri
is not a comparison category type
([cmp.categories.pre]) for any i
. The return type is deduced as the
common comparison type (see below) of R0, R1, …, Rn−1
.
在 C++20 之前,否
A function definition of the form: attribute-specifier-seq opt
decl-specifier-seq opt declarator virt-specifier-seq opt = default ;
is called an explicitly-defaulted definition. A function that is
explicitly defaulted shall
- be a special member function,
- have the same declared function type (except for possibly differing ref-qualifiers and except that in the case of a copy
constructor or copy assignment operator, the parameter type may be
“reference to non-const
T
”, where T
is the name of the member
function's class) as if it had been implicitly declared, and
- 但只有
<=>
。默认的 ==
必须 return bool
,赋值与以前的标准有类似的限制。
if F
is an assignment operator, and the return type of T1
differs
from the return type of T2
or T1
's parameter type is not a
reference, the program is ill-formed;
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]).
默认函数定义没有主体,这与引用规则冲突。无法从中推导出 return 类型,也没有规则说明在这种情况下该类型是什么。
operator<=>
有一个例外规则,指定当 auto
被使用时 return 类型将是什么,如 所示,但 operator=
没有有这样的规则。我认为没有理由不能引入这样的规则来允许默认赋值运算符中的 auto。
考虑以下 class:
class example
{
public:
auto & operator =(const example &) = default;
auto & operator =(example &&) = default;
};
这些声明是否合法?
不是语言律师回答
根据我的经验,编译器不接受默认特殊成员函数的 auto
return 类型,因此我认为它们确实是标准不允许的。
我所知道的唯一例外是 C++20 默认的三向比较运算符:
#include <compare>
struct X
{
auto operator<=>(const X&) const = default;
};
在 C++20 中,是1
Let
R
be the declared return type of a defaulted three-way comparison operator function, and letxi
be the elements of the expanded list of subobjects for an objectx
of type C`.
- If
R
isauto
, then letcvi Ri
be the type of the expressionxi <=> xi
. The operator function is defined as deleted if that expression is not usable or ifRi
is not a comparison category type ([cmp.categories.pre]) for anyi
. The return type is deduced as the common comparison type (see below) ofR0, R1, …, Rn−1
.
在 C++20 之前,否
A function definition of the form: attribute-specifier-seq opt decl-specifier-seq opt declarator virt-specifier-seq opt
= default ;
is called an explicitly-defaulted definition. A function that is explicitly defaulted shall
- be a special member function,
- have the same declared function type (except for possibly differing ref-qualifiers and except that in the case of a copy constructor or copy assignment operator, the parameter type may be “reference to non-const
T
”, whereT
is the name of the member function's class) as if it had been implicitly declared, and
- 但只有
<=>
。默认的==
必须 returnbool
,赋值与以前的标准有类似的限制。
if
F
is an assignment operator, and the return type ofT1
differs from the return type ofT2
orT1
's parameter type is not a reference, the program is ill-formed;
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]).
默认函数定义没有主体,这与引用规则冲突。无法从中推导出 return 类型,也没有规则说明在这种情况下该类型是什么。
operator<=>
有一个例外规则,指定当 auto
被使用时 return 类型将是什么,如 operator=
没有有这样的规则。我认为没有理由不能引入这样的规则来允许默认赋值运算符中的 auto。