“默认”是什么意思?意思是下课后函数声明?
我见过在类中的函数声明旁边使用 default
。它有什么作用?
class C {
C(const C&) = default;
C(C&&) = default;
C& operator=(const C&) & = default;
C& operator=(C&&) & = default;
virtual ~C() { }
};
I've seen default
used next to function declarations in a class. What does it do?
class C {
C(const C&) = default;
C(C&&) = default;
C& operator=(const C&) & = default;
C& operator=(C&&) & = default;
virtual ~C() { }
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是新的 C++11 功能。
这意味着您想要使用该函数的编译器生成的版本,因此不需要指定函数体。
您还可以使用
= delete
来指定您不希望编译器自动生成该函数。随着移动构造函数和移动赋值运算符的引入,何时生成构造函数、析构函数和赋值运算符的自动版本的规则变得相当复杂。使用
=default
和=delete
使事情变得更容易,因为您不需要记住规则:您只需说出您想要发生的事情即可。It's a new C++11 feature.
It means that you want to use the compiler-generated version of that function, so you don't need to specify a body.
You can also use
= delete
to specify that you don't want the compiler to generate that function automatically.With the introduction of move constructors and move assignment operators, the rules for when automatic versions of constructors, destructors and assignment operators are generated has become quite complex. Using
= default
and= delete
makes things easier as you don't need to remember the rules: you just say what you want to happen.这是一项新的 C++0x 功能,它告诉编译器创建相应构造函数或赋值运算符的默认版本,即仅对每个成员执行复制或移动操作的版本。这很有用,因为移动构造函数并不总是默认生成(例如,如果您有自定义析构函数),这与复制构造函数(以及同样的赋值)不同,但如果没有什么重要的东西需要编写,最好让编译器处理它而不是每次都自己拼写出来。
另请注意,如果您提供任何其他非默认构造函数,则不会生成默认构造函数。如果您仍然需要默认构造函数,则可以使用此语法让编译器创建一个。
作为另一个用例,有几种情况不会隐式生成复制构造函数(例如,如果您提供自定义移动构造函数)。如果您仍然想要默认版本,可以使用此语法请求它。
详细信息请参见标准第 12.8 节。
This is a new C++0x feature that tells the compiler to create the default version of the respective constructor or assignment operator, i.e. the one which just performs the copy or move action for each member. This is useful because the move constructor isn't always generated by default (e.g. if you have a custom destructor), unlike the copy constructor (and likewise for assignment), but if there's nothing non-trivial to write, it's better to let the compiler handle it than to spell it out yourself each time.
Also notice that a default constructor would not be generated if you provide any other non-default constructor. If you still want the default constructor, too, you can use this syntax to have the compiler make one.
As another use case, there are several situations in which a copy constructor would not be generated implicitly (e.g. if you provide a custom move constructor). If you still want the default version, you can request it with this syntax.
See Section 12.8 of the standard for details.
这是 C++11 中的新增功能,请参阅此处。如果您已经定义了一个构造函数,但想要对其他构造函数使用默认值,那么它会非常有用。在 C++11 之前,一旦定义了一个构造函数,就必须定义所有构造函数,即使它们与默认值等效。
另请注意,在某些情况下,不可能提供用户定义的默认构造函数,该构造函数的行为与编译器在默认和值初始化下合成的构造函数相同。
default
允许您恢复该行为。It is new in C++11, see here. It can be quite useful if you have defined one constructor, but want to use defaults for the others. Pre-C++11 you'd have to define all constructors once you have defined one, even if they are equivalent to the defaults.
Also note that in certain situations it is impossible to provide a user defined default constructor that behaves the same as the compiler synthesized one under both default and value initialization.
default
allows you to get that behaviour back.我在这些答案中没有看到的另一个用例是它可以轻松地允许您更改构造函数的可见性。例如,也许您希望一个友元类能够访问复制构造函数,但您不希望它公开可用。
Another use case that I do not see mentioned in these answers is that it easily allows you to change the visibility of a constructor. For example, maybe you want a friend class to be able to access the copy constructor, but you don't want it to be publicly available.
C++17 N4659 标准草案
https: //github.com/cplusplus/draft/blob/master/papers/n4659.pdf 11.4.2“显式默认函数”:
那么问题当然是哪些函数可以隐式声明以及何时发生,我已在以下位置解释过:
C++17 N4659 standard draft
https://github.com/cplusplus/draft/blob/master/papers/n4659.pdf 11.4.2 "Explicitly-defaulted functions":
Then the question is of course which functions can be implicitly declared and when does that happen, which I have explained at: