是否为 C++ 中的默认构造函数创建了汇编代码?

发布于 2024-11-05 02:02:46 字数 134 浏览 1 评论 0原文

如果我没有在 C++ 的类中定义默认构造函数或任何其他构造函数,我已经读到编译器会为您创建默认构造函数。但我创建了一个测试类,将其编译为汇编代码并检查发现没有创建任何此类内容。
有人可以澄清默认构​​造函数的代码是如何创建的,或者是否首先创建的?

If I do not define a default constructor in a class in C++ , or any other constructors, I have read that the compiler creates a default constructor for you. But I created a test class, compiled it to assembly code and checked to find that nothing of the sort is created.
Can someone clarify how the code for default constructor is created, or if it is created in the first place?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一抹苦笑 2024-11-12 02:02:46

如果您需要,则会创建默认构造函数,例如:

class Foo {
  std::string s;
};

...
Foo f;

12.1:

默认构造函数(12.1),复制
构造函数和复制赋值
运算符 (12.8) 和析构函数 (12.4)
是特殊的成员函数。这
实现将隐式声明
这些类的成员函数
当程序没有输入时键入
显式声明它们,除非
12.1 中注明。实施将
隐式定义它们,如果它们是
使用,如 12.1、12.4 和
12.8。

此外,如果您的类不需要在构造函数中执行任何操作,则编译器可能会选择不生成代码,即使按照标准构造函数应该存在。

Default constructor is created if you need it, e.g:

class Foo {
  std::string s;
};

...
Foo f;

12.1:

The default constructor (12.1), copy
constructor and copy assignment
operator (12.8), and destructor (12.4)
are special member functions. The
implementation will implicitly declare
these member functions for a class
type when the program does not
explicitly declare them, except as
noted in 12.1. The implementation will
implicitly define them if they are
used, as specified in 12.1, 12.4 and
12.8.

Also, if your class doesn't require anything to be done in the constructor, a compiler may choose to not generate the code, even though by the standard the constructor should exist.

2024-11-12 02:02:46

C++ != 汇编。

汇编是已编译的 C++ 程序的(一种可能的)输出,该程序可能包含也可能不包含某些优化,这些优化可能会省略对可能为空的构造函数的调用。

换句话说,该语言说有一个默认构造函数,但它只描述行为,而不描述实现。如果一个实现感觉不需要生成代码,那么它就不需要生成代码。

C++ != Assembly.

Assembly is (one possible) output for a compiled C++ program, which may or may not have included certain optimizations which could have elided the call to a possibly empty constructor.

In other words, the language says there's a default constructor all right, but it only describes behavior, not implementation. If an implementation feels like it doesn't need to generate code, it doesn't have to.

浸婚纱 2024-11-12 02:02:46

C++03 标准的规定如下:

§12.1/5:

类 X 的默认构造函数是可以在不带参数的情况下调用的类 X 的构造函数。如果类 X 没有用户声明的构造函数,则隐式声明默认构造函数。隐式声明的默认构造函数是其类的内联公共成员。如果构造函数是隐式声明的默认构造函数并且满足以下条件,则该构造函数是简单的:

  • 其类没有虚函数 (10.3) 和虚基类 (10.1),并且
  • 该类的所有直接基类都有简单的构造函数,并且
  • 对于其类中属于类类型(或其数组)的所有非静态数据成员,每个此类都有一个简单的构造函数。

§12.1/6:

否则,构造函数就非常重要。

§12.1/7:

类的隐式声明默认构造函数在用于创建其类类型 (1.8) 的对象时会被隐式定义。隐式定义的默认构造函数执行类的一组初始化,这些初始化将由用户为该类编写的默认构造函数执行,该类具有空的 mem-initializer-list (12.6.2) 和空的函数体。如果用户编写的默认构造函数格式不正确,则该程序也是格式错误的。在隐式定义类的隐式声明的默认构造函数之前,应已隐式定义其基类及其非静态数据成员的所有隐式声明的默认构造函数。 [注意:隐式声明的默认构造函数有一个异常规范 (15.4)。]


这意味着对于具有隐式声明但未隐式定义默认构造函数的类,或对于具有隐式定义的类>简单的默认构造函数,可能不需要生成代码。

Here's what the C++03 standard says:

§12.1/5:

A default constructor for a class X is a constructor of class X that can be called without an argument. If there is no user-declared constructor for class X, a default constructor is implicitly declared. An implicitly-declared default constructor is an inline public member of its class. A constructor is trivial if it is an implicitly-declared default constructor and if:

  • its class has no virtual functions (10.3) and no virtual base classes (10.1), and
  • all the direct base classes of its class have trivial constructors, and
  • for all the nonstatic data members of its class that are of class type (or array thereof), each such class has a trivial constructor.

§12.1/6:

Otherwise, the constructor is non-trivial.

§12.1/7:

An implicitly-declared default constructor for a class is implicitly defined when it is used to create an object of its class type (1.8). The implicitly-defined default constructor performs the set of initializations of the class that would be performed by a user-written default constructor for that class with an empty mem-initializer-list (12.6.2) and an empty function body. If that user-written default constructor would be illformed, the program is ill-formed. Before the implicitly-declared default constructor for a class is implicitly defined, all the implicitly-declared default constructors for its base classes and its nonstatic data members shall have been implicitly defined. [Note: an implicitly-declared default constructor has an exception-specification (15.4).]


The implication is that for classes with implicitly declared but not implicitly defined default constructors, or for classes with implicitly defined trivial default constructors, no code generation may be necessary.

尴尬癌患者 2024-11-12 02:02:46

您是否想问您的编译器是否确实为默认构造函数发出代码?

这取决于优化。大多数现代编译器在与 -O0 一起使用时会发出默认的构造函数代码序列,但如果未使用它并且您使用 -O2 或更高版本,则会对其进行优化。

Did you want to ask if your compiler actually emits code for a default constructor?

That depends on the optimization. Most modern compilers will emit the default constructor code sequence when used with -O0, but will optimize it away if it is unused and you use -O2 or higher.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文