默认构造函数和析构函数是否是内联的? [英] Are the default constructor and destructor ever inline?

查看:156
本文介绍了默认构造函数和析构函数是否是内联的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇编译器生成的默认构造函数和析构函数是否是内联的,因为我可以用任何一种方式对其进行辩护.一方面,您希望默认构造函数/析构函数不内联,以便以后添加它们不会破坏ABI(因为仅当存在默认值时编译的目标文件才会内联生成的定义而不是您定义的内容).另一方面,对于C ++编译器来说,编译C语言的性能与使用C编译器编译时一样,不能为每个分配的结构添加构造函数/析构函数调用,而在C ++中,类之间的唯一功能区别是struct应该是默认的访问保护.也许链接器以某种方式解决了这个问题?答案可能会因编译器而异?

I'm curious if the default constructor and destructor that the compiler generates are inline or not, because I can justify it either way. On the one hand, you want the default constructor/destructor to not be inline so that adding them later doesn't break ABI (because object files compiled when only the defaults were there will have inlined the generated definitions instead of what you define). On the other hand, for a C++ compiler to compile C code that performs as well as when compiled with a C compiler, it can't be adding constructor/destructor calls for every allocated struct, and in C++ the only functional difference between a class and a struct is supposed to be the default access protection. Maybe the linker addresses this somehow? Maybe the answer varies across compilers?

这个问题的结果:如果我在C ++中具有POD结构,那么从理论上讲,我可以通过自己定义空的内联构造函数/析构函数来代替默认值而在某些编译器中受益吗?

A consequence of this question: if I have a POD struct in C++, can I theoretically benefit under some compilers by defining empty inline constructor/destructors myself in place of the defaults?

推荐答案

C ++标准在 12.1 [class.ctor]/5

一个隐式声明的默认构造函数是其类的内联公共成员

An implicitly-declared default constructor is an inline public member of its class

12.4 [class.dtor]/3

一个隐式声明的 析构函数是该类的嵌入式公共成员.

An implicitly-declared destructor is an inline public member of its class.

这篇关于默认构造函数和析构函数是否是内联的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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