10

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?

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?

Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165

3 Answers3

19

The C++ standard says, in 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

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

Cubbi
  • 46,567
  • 13
  • 103
  • 169
  • Which I take to mean much the same as the `inline` keyword - that the compiler's free to exercise it's own judgement and need *not* actually inline the constructor if it's excessively large... is that your understanding too? – Tony Delroy Jun 08 '11 at 09:08
  • @Tony I would think so, yes. 7.1.2/2 says, self-referentially, "An implementation is not required to perform this inline substitution at the point of call; however, even if this inline substitution is omitted, the other rules for inline functions defined by 7.1.2 shall still be respected" – Cubbi Jun 08 '11 at 10:09
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?

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.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • This doesn't address my question. I'm asking whether the default compiler generated constructor/destructor are inline. – Joseph Garvin Jun 08 '11 at 03:47
  • @Joseph Garvin: I believe that will vary from compiler to compiler and what how does it affect the way you write your implementations in anyway? – Alok Save Jun 08 '11 at 03:50
  • @Als: I edited my question to include one way it could matter at the bottom. – Joseph Garvin Jun 08 '11 at 03:52
  • Alas, according to Cubbi's citation of the standard it looks like the real answer is no. The standard already defines them as inline. – Joseph Garvin Jun 08 '11 at 03:56
  • 1
    @Joseph Garvin: The point is that "are inline" isn't really meaningful in this context. The `inline` keyword applied to a function **does not guarantee** that the function will be inlined, and the absence of the `inline` marking (whether done explicitly using the keyword, or implicitly), **does not prevent** the function from being inlined. It is only a hint. – Karl Knechtel Jun 08 '11 at 04:25
  • 1
    @Karl: Generally a good point - +1ed - but I believe it's not entirely correct... for instance, an out-of-line function compiled into a shared library can not be inlined by client code outside that shared library during compilation or linking, otherwise varying the shared library wouldn't reliably vary the implementation. To ensure this, inlining doesn't normally cross translation unit boundaries until link time, and then only statically linked objects can consider such inlining... from a tool perspective - a painful time to have to perform it. – Tony Delroy Jun 08 '11 at 09:13
  • @Tony yes, a +1 for you also - link time optimization is not an easy thing, AFAIK :) – Karl Knechtel Jun 08 '11 at 22:51
  • @Karl: From an ABI point of view though, that you can't know what the compiler will do means you have to be conservative and assume that it has inlined the function. That's one reason why it's good to know the answer to this question. – Joseph Garvin Jun 12 '11 at 21:08
2

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.

John Ripley
  • 4,434
  • 1
  • 21
  • 17