简体   繁体   English

具有编译器删除的默认特殊成员函数的格式良好的类定义的示例

[英]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 function F is allowed to differ from the type T 2 it would have had if it were implicitly declared, as follows: 2 允许显式默认的特殊成员函数F的类型T 1与隐式声明的类型T 2不同,如下所示:

(2.1) — T 1 and T 2 may have differing ref-qualifiers; (2.1) — T 1T 2可能有不同的引用限定符;

(2.2) — T 1 and T 2 may have differing exception specifications; (2.2)—— T 1T 2可能有不同的例外规范; and

(2.3) — if T 2 has a parameter of type const C& , the corresponding parameter of T 1 may be of type C& . (2.3) — 如果T 2有一个const C&类型的参数,则T 1的相应参数可能是C&类型。

If T 1 differs from T 2 in any other way, then:如果T 1T 2以任何其他方式不同,则:

(2.4) — if F is an assignment operator, and the return type of T 1 differs from the return type of T 2 or T 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 1Wrapper的复制构造函数)与它隐式声明的情况不同(根据[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 1const&但没有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.

相关问题 带外部定义的默认特殊成员 function - Defaulted special member function with external definition 为什么类模板的成员函数声明必须格式正确? - Why should the member function declarations of a class template be all well-formed? 默认参数格式良好后是可变参数吗? - Are variadic arguments after a defaulted parameter well-formed? 格式良好的程序包含格式错误的模板成员函数? - Well-formed program containing an ill-formed template member function? 在另一个成员函数的尾随返回类型中获得推断成员函数的decltype是否格式良好? - Is getting the decltype of a deduced member function inside the trailing return type of another member function well-formed? 执行明确默认的特殊成员函数生成 - Enforcing explicitly defaulted special member function generation 可以删除(或默认)非特殊C ++成员函数吗? - Can non-special C++ member functions be deleted (or defaulted)? 为什么课堂上的部分专业化很好? - Why is in-class partial specialization well-formed? 复制/移动省略是否允许使用格式正确的删除函数制作程序? - Is copy/move elision allowed to make a program using deleted functions well-formed? 在通用lambda中使用`if constexpr`访问成员类型需要两个分支都是格式良好的 - gcc vs clang - Accessing member type with `if constexpr` inside generic lambda requires both branches to be well-formed - gcc vs clang
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM