4

Having a class, that contains private member function, I would like to mark as inline (to explicitely mark my intent of hint that particular function may be inlined), but don't expose their implementation to an API consumer. For simplicity, it might look like:

Airplane.h:

class Airplane
{
    char manufacturer[80];
    char mode[80];
    // ... 
public:
    void autopilot_steer_left(int degree);
    // ...
private:
    // ...
    inline bool validate_hydraulic_on_left_wing();  // secret, patent-based, etc.
};

Airplane.cpp:

#include "Airplane.h"
// ...

void Airplane::autopilot_steer_left(int degree)
{
    // ...
}

Where should I put definition of Airplane::validate_hydraulic_on_left_wing? I know that common place is Airplane.h (possibly inside class, so it is inline implicitely), but this contradicts with my intent.

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
  • 5
    Just don't mark it `inline`? See [when should I write the keyword `inline`](http://stackoverflow.com/q/1759300/2069064) – Barry Jul 20 '15 at 18:38
  • inline isn't doing what you seem to expect ... – Daniel Jour Jul 20 '15 at 18:40
  • 1
    Usually (not necessarily in your case) either the `inline` function is very simple so there is nothing to hide, or it is not simple but it should not be `inline` in this case. – dlask Jul 20 '15 at 18:41
  • _"I would like to mark some of them as inline (to explicitely mark my intent of hint that this particular function may be inlined)"_ Don't bother mate – Lightness Races in Orbit Jul 20 '15 at 18:47
  • _"but don't expose their implementation to an API consumer"_ Literally the opposite of what inlining does. This question makes no sense. – Lightness Races in Orbit Jul 20 '15 at 18:48

3 Answers3

5

You can use the keyword inline in the .cpp file:

inline void Airplane::autopilot_steer_left(int degree)
{
    // ...
}

Thus your implementation will be hidden in the .cpp file and inline as well.

As to the benefits of doing so, however, that is debatable. Here is a good discussion relating to inline performance: Benefits of inline functions in C++?

Community
  • 1
  • 1
edtheprogrammerguy
  • 5,957
  • 6
  • 28
  • 47
  • @DanielJour - I was just answering his question as to how to have the implementation `inline`, but not in the .h file. – edtheprogrammerguy Jul 20 '15 at 18:43
  • @DanielJour Maybe the link-time optimization can inline it? – KABoissonneault Jul 20 '15 at 18:45
  • 3
    @KABoissonneault LTO (if available) will probably inline it, though it won't do that based on the presence of the inline keyword, but rather on it's own metrics. – Daniel Jour Jul 20 '15 at 18:49
  • 1
    @edtheprogrammerguy it's just that inline in the source file won't have the same (presumed, I.e. this was true with compilers before around 2000) effects as when placed in the header file. Back then this would have hinted to inline that function only inside that translation unit, but not in the others, as was the case with inline in the header. Nowadays I'd just write it without inline keyword. – Daniel Jour Jul 20 '15 at 18:53
  • @DanielJour - I agree. Doesn't make much sense to use it anymore. – edtheprogrammerguy Jul 20 '15 at 18:55
4

If the function cannot be called from outside of a single translation unit (i.e. cpp file), then there is often no real sense in marking it inline.

From C++ perspective, inline modifier only allows function to be defined in several units. There is no more semantics behind inline keyword.

From perspective of real inlining of function calls (i.e. compiler optimization), it may only be useful if your compiler is configured to never inline functions that are not marked as inline (like /Ob1 for Visual C++). However, it is quite normal for a modern compiler to try to inline all functions it can. In this case the compiler does not even look whether you have marked the function as inline or not: any call within a single unit would be inlined if compiler thinks it makes code better.

stgatilov
  • 5,333
  • 31
  • 54
3

This makes no sense. The entire point of inlining functions is to ensure that the definition is available at the callsite to directly replace the call. That is 100% the opposite of what you want to do: to hide the definition away from the callsite.

It doesn't matter anyway, since inline is totally ignored for inlining purposes, and is only used to dictate some relaxed linkage requirements. But, as it is, your function is already not going to be inlined, whether you write inline on it or not.

Just remove the inline keyword, and define the function externally as you do now.

Then turn on link-time optimisation and hope your compiler decides to inline the function definition at link-time. That's the very latest you can get the function inlined, and hopefully that's enough "hiding" for your desires.

If not, well, does it really matter?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055