19

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)?

j4nu5
  • 335
  • 3
  • 13
  • 18
    These days, `inline` declarations have very little (nothing) to do with a compiler's inlining. It's mainly a curious way of spelling "this is allowed to have more than one definition". – molbdnilo Dec 21 '17 at 14:05
  • 6
    To allow (implicit) definition in multiple translation units, as always. – cpplearner Dec 21 '17 at 14:05

2 Answers2

32

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).

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
30

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.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • 2
    Thank for actually fully answering the question. +1 – user541686 Dec 21 '17 at 23:26
  • For more info : https://akrzemi1.wordpress.com/2014/07/14/inline-functions/ – j4nu5 Dec 22 '17 at 14:32
  • Effective C++, Item #30 Take the default constructor as an example, it is not recommended to declare it as inline. So since the constructor is inline by default, this example does not exist? – daohu527 Sep 01 '21 at 03:22