如何“=默认”不同于“{}”为默认构造函数和析构函数? [英] How is "=default" different from "{}" for default constructor and destructor?

查看:208
本文介绍了如何“=默认”不同于“{}”为默认构造函数和析构函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初发布这个问题只是关于析构函数,但现在我添加了默认构造函数的考虑。这是原来的问题:


如果我想给我的类一个析构函数是虚拟的,但是
否则相同编译器会生成什么,我可以使用 = default

  Widget {
public:
virtual〜Widget()= default;
};

但是似乎我可以得到相同的效果与更少的打字使用
空定义:

  class Widget {
public:
virtual〜Widget(){}
} ;

这两个定义的行为有什么不同吗?


根据为这个问题发布的回复,默认构造函数的情况看起来类似。考虑到对于析构函数, = default {} 之间的含义几乎没有差别类似地,在默认构造函数的这些选项之间的意义上几乎没有差别?也就是说,假设我想创建一个类型,其中该类型的对象将被创建和销毁,我为什么要说

  Widget()= default; 

而不是

  Widget(){} 



如果在原始帖子违反了一些SO规则之后扩展此问题,我们深表歉意。

解决方案

这是一个完全不同的问题,当询问关于构造函数而不是析构函数。



如果你的析构函数是 virtual ,那么差异是可以忽略的, Howard指出。但是,如果你的析构函数是<非>虚拟,这是一个完全不同的故事。



对特殊成员函数使用 = default 语法(默认构造函数,复制/移动构造函数/赋值,析构函数等等)意味着与只是简单地做 {} 不同。使用后者,该功能变为用户提供。



这是C ++ 11定义的一个简单类:

  struct Trivial 
{
int foo;
};

如果尝试默认构造一个,编译器将自动生成一个默认构造函数。同样适用于复制/移动和销毁。因为用户没有提供任何这些成员函数,C ++ 11规范认为这是一个琐碎的类。



这是:

  struct NotTrivial 
{
int foo;

NotTrivial(){}
};

顾名思义,这不再是微不足道的。它有一个由用户提供的默认构造函数。它是空的无关紧要;



这是:

pre> struct Trivial2
{
int foo;

Trivial2()= default;
};

顾名思义,这是一个简单的类型。为什么?因为你告诉编译器自动生成默认构造函数。因此,构造函数不是用户提供的。因此,类型计算为微不足道,因为它没有用户提供的默认构造函数。



=默认语法主要用于做复制构造函数/赋值等事情,当你添加成员函数阻止创建这样的函数。但它也会触发编译器的特殊行为,因此它在默认构造函数/析构函数中也很有用。


I originally posted this as a question only about destructors, but now I'm adding consideration of the default constructor. Here's the original question:

If I want to give my class a destructor that is virtual, but is otherwise the same as what the compiler would generate, I can use =default:

class Widget {
public:
   virtual ~Widget() = default;
};

But it seems that I can get the same effect with less typing using an empty definition:

class Widget {
public:
   virtual ~Widget() {}
};

Is there any way in which these two definitions behave differently?

Based on the replies posted for this question, the situation for the default constructor seems similar. Given that there is almost no difference in meaning between "=default" and "{}" for destructors, is there similarly almost no difference in meaning between these options for default constructors? That is, assuming I want to create a type where the objects of that type will be both created and destroyed, why would I want to say

Widget() = default;

instead of

Widget() {}

?

I apologize if extending this question after its original posting is violating some SO rules. Posting an almost-identical question for default constructors struck me as the less desirable option.

解决方案

This is a completely different question when asking about constructors than destructors.

If your destructor is virtual, then the difference is negligible, as Howard pointed out. However, if your destructor was non-virtual, it's a completely different story. The same is true of constructors.

Using = default syntax for special member functions (default constructor, copy/move constructors/assignment, destructors etc) means something very different from simply doing {}. With the latter, the function becomes "user-provided". And that changes everything.

This is a trivial class by C++11's definition:

struct Trivial
{
  int foo;
};

If you attempt to default construct one, the compiler will generate a default constructor automatically. Same goes for copy/movement and destructing. Because the user did not provide any of these member functions, the C++11 specification considers this a "trivial" class. It therefore legal to do this, like memcpy their contents around to initialize them and so forth.

This:

struct NotTrivial
{
  int foo;

  NotTrivial() {}
};

As the name suggests, this is no longer trivial. It has a default constructor that is user-provided. It doesn't matter if it's empty; as far as the rules of C++11 are concerned, this cannot be a trivial type.

This:

struct Trivial2
{
  int foo;

  Trivial2() = default;
};

Again as the name suggests, this is a trivial type. Why? Because you told the compiler to automatically generate the default constructor. The constructor is therefore not "user-provided." And therefore, the type counts as trivial, since it doesn't have a user-provided default constructor.

The = default syntax is mainly there for doing things like copy constructors/assignment, when you add member functions that prevent the creation of such functions. But it also triggers special behavior from the compiler, so it's useful in default constructors/destructors too.

这篇关于如何“=默认”不同于“{}”为默认构造函数和析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆