Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are the default constructor and destructor of a class inline?

Tags:

c++

I read from multiple sources that:

If no user-declared constructors of any kind are provided for a class type (struct, class, or union), the compiler will always declare a default constructor as an inline public member of its class.

Why was this decision made (to explicitly declare ctors/dtors as inline)? Compilers are free to inline / non-inline this anyways? Especially since inlining ctors may have a huge penalty on the clients of a class (Effective C++, Item #30)?

like image 648
j4nu5 Avatar asked Dec 21 '17 14:12

j4nu5


People also ask

Can a destructor be inline?

If your base class has virtual constructor/destructor yours will probably never be inlined. btw, virtual member functions (including virtual destructors) may be inlined if the compiler knows the (complete) type of the object being destructed.

Can we declare constructor as inline?

Constructors may be declared as inline , explicit , friend , or constexpr . A constructor can initialize an object that has been declared as const , volatile or const volatile .

Can constructor and destructor be declared as virtual?

Constructors cannot be declared with the keyword virtual . Constructors and destructors cannot be declared static , const , or volatile .

Why does a class need to have a default constructor?

What is the significance of the default constructor? They are used to create objects, which do not have any specific initial value. Is a default constructor automatically provided? If no constructors are explicitly declared in the class, a default constructor is provided automatically by the compiler.


2 Answers

They're not inline in the sense "they will always be inlined by the compiler." They are inline in the sense "considered defined in every translation unit which sees the class definition, without violating the One Definition Rule (ODR)." Note that the latter is the only meaning of the phrase "inline function" which standard C++ uses.

Explicitly marking a function with the inline keyword is also a non-binding hint to the compiler to actually inline the function, but I doubt modern compilers & optimisers pay much attention to this hint. However, note that this (hint to inline) applies only to using the keyword inline, and not to functions implicitly inline (such as the defaulted constructor and destructor mentioned in the question).

like image 51
Angew is no longer proud of SO Avatar answered Sep 21 '22 12:09

Angew is no longer proud of SO


inline has two meanings in the C++ standard.

The first is what you think of when you hear inline; taking the code in the function and injecting it into the place where it is called.

The C++ standard advises implementations to do this when they see an inline method or function, but does not require it. As such action has zero observable behavior changes in the abstract machine that the C++ standard describes, I consider it non-normative advice.

The second has to do with linking. An inline function (or in C++17 a variable) can exist in multiple translation units. Normally this causes an error at link-time; but when the variable or function is inline, instead all but one of the instances of the variable or function are silently discarded. If they differ in any important way, this makes your program ill-formed no diagnostic required.

This second meaning is why implicit ctors and dtors are implicitly inline; it means that no single translation unit has to be chosen for them to "live in". Instead, they are generated everywhere they are needed. They may be preferentially actually inlined into calling code, but most importantly if any vestigial copies of it still exist (because it was not inlined, say), no error occurs at link time, and instead all but one of them are discarded.

See inline in the C++ standard. The wording in the standard is a bit harder to understand, different and more precise than I use above.

like image 20
Yakk - Adam Nevraumont Avatar answered Sep 22 '22 12:09

Yakk - Adam Nevraumont