[英]An example of a well-formed class definition with a defaulted special member function that the compiler makes deleted
In the C++20 standard, [dcl.fct.def.default] , Explicitly-defaulted functions:在 C++20 标准中, [dcl.fct.def.default] ,显式默认函数:
2 The type
T 1
of an explicitly defaulted special member functionF
is allowed to differ from the typeT 2
it would have had if it were implicitly declared, as follows:2 允许显式默认的特殊成员函数
F
的类型T 1
与隐式声明的类型T 2
不同,如下所示:(2.1) —
T 1
andT 2
may have differing ref-qualifiers;(2.1) —
T 1
和T 2
可能有不同的引用限定符;(2.2) —
T 1
andT 2
may have differing exception specifications;(2.2)——
T 1
和T 2
可能有不同的例外规范; and和
(2.3) — if
T 2
has a parameter of typeconst C&
, the corresponding parameter ofT 1
may be of typeC&
.(2.3) — 如果
T 2
有一个const C&
类型的参数,则T 1
的相应参数可能是C&
类型。If
T 1
differs fromT 2
in any other way, then:如果
T 1
与T 2
以任何其他方式不同,则:(2.4) — if
F
is an assignment operator, and the return type ofT 1
differs from the return type ofT 2
orT 1
's parameter type is not a reference, the program is ill-formed;(2.4) — 如果
F
是赋值运算符,并且T 1
的返回类型与T 2
的返回类型不同或T 1
的参数类型不是引用,则程序格式错误;(2.5) — otherwise, if
F
is explicitly defaulted on its first declaration, it is defined as deleted;(2.5) —否则,如果
F
在其第一次声明时被明确默认,则将其定义为已删除;(2.6) — otherwise, the program is ill-formed
(2.6) — 否则,程序格式错误
Could anybody provide an example of a special member function explicitly defaulted and that is deleted by the compiler.任何人都可以提供一个特殊成员函数的示例,该函数明确默认并被编译器删除。 The function declaration should be well-formed.
函数声明应该是格式良好的。
The example from P0641 , whence this wording:来自P0641的例子,因此这个措辞:
struct MyType {
MyType(MyType&); // no 'const'
};
template <typename T>
struct Wrapper {
Wrapper(const Wrapper&) = default;
T t;
};
Wrapper<MyType> var; // fails to instantiate
Pretend there was actually a default constructor.假设实际上有一个默认构造函数。
This was previously ill-formed.这是以前格式错误的。 Now,
T 1
( Wrapper
's copy constructor) differs from it what it would have been were it implicitly declared (would have been Wrapper(Wrapper&)
per [class.copy.ctor]/7 ).现在,
T 1
( Wrapper
的复制构造函数)与它隐式声明的情况不同(根据[class.copy.ctor]/7本来应该是Wrapper(Wrapper&)
)。 This doesn't match the cases in the first set of bullets (here T 1
has const&
but not T 2
, the bullet is in the opposite order), so we fall through to the second set of bullets - and we end up with Wrapper<MyType>
's copy constructor being deleted.这与第一组项目符号中的情况不匹配(这里
T 1
有const&
但没有T 2
,项目符号的顺序相反),所以我们落入第二组项目符号 - 最后我们得到了Wrapper<MyType>
的复制构造函数被删除。
A good example of where this would've come up in code is something like std::tuple
(see LWG2086 ), where prior to these changes:在代码中出现这种情况的一个很好的例子是
std::tuple
(参见LWG2086 ),在这些更改之前:
struct A {
A();
A(A&);
};
std::tuple<A> x; // ill-formed
Now, this is well-formed, just that tuple<A>
isn't copyable.现在,这是格式良好的,只是
tuple<A>
不可复制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.