C++ 类的函数声明后“默认”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6502828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:16:50  来源:igfitidea点击:

What does "default" mean after a class' function declaration?

c++c++11defaultdeclarationkeyword

提问by Paul Manta

I've seen defaultused next to function declarations in a class. What does it do?

我见过default在类中的函数声明旁边使用。它有什么作用?

class C {
  C(const C&) = default;
  C(C&&) = default;
  C& operator=(const C&) & = default;
  C& operator=(C&&) & = default;
  virtual ~C() { }
};

回答by Peter Alexander

It's a new C++11 feature.

这是一个新的 C++11 特性

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 = deleteto specify that you don'twant the compiler to generate that function automatically.

您还可以使用= delete指定您希望编译器自动生成该函数。

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 = defaultand = deletemakes things easier as you don't need to remember the rules: you just say what you want to happen.

随着移动构造函数和移动赋值运算符的引入,何时生成构造函数、析构函数和赋值运算符的自动版本的规则变得相当复杂。使用= default= delete使事情变得更容易,因为您不需要记住规则:您只需说出您想要发生的事情。

回答by Kerrek SB

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.

这是一个新的 C++0x 特性,它告诉编译器创建相应构造函数或赋值运算符的默认版本,即只为每个成员执行复制或移动操作的那个。这很有用,因为移动构造函数并不总是在默认情况下生成(例如,如果您有自定义析构函数),与复制构造函数不同(并且同样用于赋值),但是如果没有任何重要的东西要写,最好让编译器处理它而不是每次都自己拼写出来。

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.

有关详细信息,请参阅标准的第 12.8 节。

回答by juanchopanza

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.

它是 C++11 中的新功能,请参见此处。如果您定义了一个构造函数,但想为其他构造函数使用默认值,它会非常有用。在 C++11 之前,一旦定义了一个构造函数,您就必须定义所有构造函数,即使它们等同于默认值。

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 defaultand valueinitialization. defaultallows you to get that behaviour back.

另请注意,在某些情况下,不可能提供与编译器在默认初始化下合成的行为相同的用户定义的默认构造函数。default允许您恢复该行为。

回答by dshin

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.

我在这些答案中没有提到的另一个用例是,它允许您轻松更改构造函数的可见性。例如,也许您希望友元类能够访问复制构造函数,但您不希望它公开可用。