4

From what I read somewhere long time ago, it seems that if you want class member function to be inlined during the compilation phase, the function has to be defined inside class declaration block.

But this has a downside of a detail leak. IMHO, other programmers should only see class interface when opening .h file.

Is the first statement still true in modern C++, was it ever? Is there a way to force inlining for functions that are declared, preferably in another file altogether?

Is it generally better to keep short member functions inside class declaration block, or not?

Coder
  • 3,405
  • 5
  • 25
  • 40
  • So long as C++ requires you to declare every member and method in the class definition, you're going to have detail leak. – Mike DeSimone Jun 03 '12 at 01:39
  • "Detail leak" is one of my big objections to C++ (in contrast, for example, to Ada or Pascal). In his book, "The C++ Programming Language", Stroustrup argues otherwise. But heck, he also argues that "C++ users need to know only about the subset of the language explicitly used to write a program." Simply not true ;) – paulsm4 Jun 03 '12 at 01:48
  • 1
    you can hid implementation detail with pimpl, if you can be bothered – wreckgar23 Jun 03 '12 at 01:55
  • @wreckgar23: Yes, you have that option, but then the function cannot be inlined (other than by whole program optimization if your compiler does it). You have to choose: hide details vs. potentially better performance. That is the more information available for the compiler, the greater chances of optimizing, the more you hide inside a single translation unit, the less chances the compiler has of optimizing in other translation units.... – David Rodríguez - dribeas Jun 03 '12 at 02:03
  • @dribeas a pimpl function may be inlined by the compiler, as you say (but with different emphasis :) ) inlining is not necessarily the same as optimising for performance (it can be the opposite), so I'd be minded to establish that I need to optimise first. – wreckgar23 Jun 03 '12 at 12:04

3 Answers3

7

It seems that if you want class member function to be inlined during the compilation phase, the function has to be defined inside class declaration block.

That is not really true. A function that is defined inside the class definition is implicitly marked as inline. But you don't need to defined the function inside the class for it to be inline, you can explicitly request it:

struct X {
   void f();
};
inline void f() {}

The inline keyword on the other hand, does not mean that the function will be inlined, but rather that it can be defined in multiple translation units, that is, if multiple translation units include the same header that contains that definition, the linker will not fail with a multiple definition error.

Now, on actual inlining, the compiler can decide to inline or not any function, regardless of whether the function is declared as inline provided that it sees the definition of that function (the code that it will inline), which is the reason why in general functions that are meant to be inlined should be defined in the header (either inside the class definition or marked inline outside.

Additionally, newer toolchains can perform whole program optimization or other link time optimizations, by which the linker can also decide that a function should be inlined. In this case, the function definition needs not be visible at the call site, so it could be defined inside the .cpp file. But if you really want the function to be inlined it is better not to depend on this feature and just define the function in the header.

David Rodríguez - dribeas
  • 192,922
  • 20
  • 275
  • 473
2

Q: Is there a way to force inlining for functions?

A: No

No matter how you designate a function as inline, it is a request that the compiler is allowed to ignore: it might inline-expand some, all, or none of the calls to an inline function.

Q: What are the inlining rules within C++ classes?

Inline member functions in C++

As far as Standard C++ is concerned, a inline function must be defined in every translation unit in which it is used ...

This is different from non-inline functions which must be defined only once in an entire program (one-definition-rule)...

For member-functions, if you define your function in the class, it is implicitly inline. And because it appears in the header, the rule that it has to be defined in every translation unit in which it is used is automatically satisfied.

Here is a great FAQ (one that's more "practical" than "pedantic"):

http://www.parashift.com/c++-faq-lite/inline-functions.html

Community
  • 1
  • 1
paulsm4
  • 99,714
  • 15
  • 125
  • 160
  • Yes, there is a way to force inline of a method and/or function. –  Jun 03 '12 at 01:39
  • @VladLazarenko: There is no standard way. Forcing inlining can only be done (currently) by means of compiler specific ways of doing it, but no standard portable way. – David Rodríguez - dribeas Jun 03 '12 at 01:44
  • @DavidRodríguez-dribeas: Right, but it can be done. And it is being done a lot. –  Jun 03 '12 at 02:05
0

Is the first statement still true in modern C++, was it ever?

As David explained, there's the inline keyword as well. It can be ignored, as Paul stated.

Is there a way to force inlining for functions that are declared, preferably in another file altogether?

Probably by configuring your compiler. It might be doing some inling behind your back anyway. Eg. gcc has -finline-functions etc. that will be switched on for certain optimisation levels

Is it generally better to keep short member functions inside class declaration block, or no?

Up to you. Be aware though that if you have an inline method used lots of times, then you can be increasing the size of your object files, and so potentially bloat the size of what you're building and maybe slow it down.

FWIW I only tend to put implementations in header files out of laziness :)

wreckgar23
  • 985
  • 8
  • 22