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?
我很好奇编译器生成的默认构造函数和析构函数是否是内联的,因为我可以用任何一种方式来证明它。一方面,您希望默认的构造函数/析构函数不被内联,以便以后添加它们不会破坏ABI(因为在只有默认值时编译的对象文件将内联生成的定义,而不是定义的定义)。另一方面,对于一个c++编译器来编译C代码执行以及与C编译器编译时,不能添加构造函数和析构函数要求每一个分配结构,和在c++中唯一的功能区别类和结构体应该是默认的访问保护。也许链接器以某种方式解决了这个问题?也许答案会因编译器而异?
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++中有一个POD结构体,那么在某些编译器下,我是否可以通过定义空的内联构造函数/析构函数来代替默认值,从而在理论上获益?
3 个解决方案
#1
16
The C++ standard says, in 12.1[class.ctor]/5
c++标准是12.1[class.ctor]/5
An implicitly-declared default constructor is an inline public member of its class
隐式声明的默认构造函数是其类的内联公共成员
and in 12.4[class.dtor]/3
和12.4(class.dtor)/ 3
An implicitly-declared destructor is an inline public member of its class.
隐式声明的析构函数是其类的内联公共成员。
#2
3
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++中有一个POD结构体,那么在某些编译器下,我可以自己定义空的内联构造函数/析构函数来代替默认值吗?
Theorotically, Yes! Any function(including constructors & destructors) can be declared inline, and putting the function body in the class definition is one way of doing that. However, it's up to the compiler if it actually does inline the function.
Theorotically,是的!任何函数(包括构造函数和析构函数)都可以内联地声明,将函数体放到类定义中是实现这一点的一种方法。但是,如果编译器实际执行内联函数,则由编译器决定。
#3
1
It varies across compilers, but in general: yes, they should.
不同的编译器有不同,但总的来说:是的,他们应该这样做。
With gcc at least, you get both an inline and an out-of-line function generated. The out-of-line version is marked as "link once", so no matter how many objects generate a default constructor, at most only one version will end up in the linked output. If in fact nobody uses the default constructor out-of-line, it's not included in the linked output at all, and you have effectively a purely inline function.
至少使用gcc,您可以生成一个内联函数和一个内联函数。离线版本被标记为“链接一次”,因此不管有多少对象生成一个默认构造函数,最多只有一个版本会在链接输出中结束。如果实际上没有人使用内联的默认构造函数,那么它根本不包含在链接的输出中,并且您实际上有一个纯粹的内联函数。
#1
16
The C++ standard says, in 12.1[class.ctor]/5
c++标准是12.1[class.ctor]/5
An implicitly-declared default constructor is an inline public member of its class
隐式声明的默认构造函数是其类的内联公共成员
and in 12.4[class.dtor]/3
和12.4(class.dtor)/ 3
An implicitly-declared destructor is an inline public member of its class.
隐式声明的析构函数是其类的内联公共成员。
#2
3
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++中有一个POD结构体,那么在某些编译器下,我可以自己定义空的内联构造函数/析构函数来代替默认值吗?
Theorotically, Yes! Any function(including constructors & destructors) can be declared inline, and putting the function body in the class definition is one way of doing that. However, it's up to the compiler if it actually does inline the function.
Theorotically,是的!任何函数(包括构造函数和析构函数)都可以内联地声明,将函数体放到类定义中是实现这一点的一种方法。但是,如果编译器实际执行内联函数,则由编译器决定。
#3
1
It varies across compilers, but in general: yes, they should.
不同的编译器有不同,但总的来说:是的,他们应该这样做。
With gcc at least, you get both an inline and an out-of-line function generated. The out-of-line version is marked as "link once", so no matter how many objects generate a default constructor, at most only one version will end up in the linked output. If in fact nobody uses the default constructor out-of-line, it's not included in the linked output at all, and you have effectively a purely inline function.
至少使用gcc,您可以生成一个内联函数和一个内联函数。离线版本被标记为“链接一次”,因此不管有多少对象生成一个默认构造函数,最多只有一个版本会在链接输出中结束。如果实际上没有人使用内联的默认构造函数,那么它根本不包含在链接的输出中,并且您实际上有一个纯粹的内联函数。