0

(I'm using gnu g++ v5.2.1 and C++11, but this is a generic C++ question.)

I'd like to put a few trivial methods in my .hpp files. Many .cpp files in my project include this .hpp file:

class foo
{
    public:

        int        example1( int i ) { return i + 5; }
        inline int example2( int i ) { return i + 5; }
};

Is there a difference between example1() and example2() when one is explicitly declared inline and the other one doesn't mention inline?

Should I prefer one over the other?


Edit: No, @HostileFork, that other question you've marked as a possible duplicate doesn't answer my questions.

Stéphane
  • 19,459
  • 24
  • 95
  • 136
  • 2
    Possible duplicate of [When should I write the keyword 'inline' for a function/method?](http://stackoverflow.com/questions/1759300/when-should-i-write-the-keyword-inline-for-a-function-method) – HostileFork says dont trust SE Mar 30 '16 at 02:03
  • 3
    Those are the same. Class member functions are automatically inline if their definition appears in the header. You only need to mark non-member functions `inline` if you want to define then in the header file rather than in a source file (.cpp) to give the compiler opportunities to optimize calls. – Galik Mar 30 '16 at 02:08
  • @HostileFork the answers there only seem to talk about non-member functions – M.M Mar 30 '16 at 02:13
  • 1
    @Stéphane I don't know what part of your question isn't answered by it. You were presumably aware before asking that you didn't need to add the inline to get the semantics of inlining (e.g. you won't get linker errors from multiple inclusions of a class with method definitions in a header without the inline marker). So beyond that you're asking about whether the compiler pays attention to inline for any optimization reason or not. Answer there is: most compilers throw out inline except for the semantic aspect these days. – HostileFork says dont trust SE Mar 30 '16 at 02:24

3 Answers3

3

Member functions with their body inside the class definition are implicitly inline, so your two code samples are the same.

However it is different if the body is provided out-of-line:

class foo
{
    public:
        int example1( int i );
        inline int example2( int i );
};

In this code, foo::example2 is an inline function. The out-of-line definition must also appear in the header (unless the function is never odr-used). It's optional whether the out-of-line definition also includes the inline specifier.

For foo::example1, if there is an out-of-line definition with inline specifier, then the function is inline. Again, if such a definition is present then it must be in the header unless the function is never odr-used.

These ODR violations are undefined behaviour with no diagnostic required. In practice this is likely to mean it may "appear to work", or give a link error.


Typically, you would only use my example2 case where you want the function definition to appear in the header but you think it is more readable to have the class definition compact and the function body later on.

In this case I would recommend using inline in both the declaration and definition, to enhance readability.

Note for Windows programming: If the class is marked __declspec(dllexport) or __declspec(dllimport), then gcc warns about example1 . I'm not sure what the correct behaviour is here but to avoid any possible issues, use inline in the declaration.

M.M
  • 138,810
  • 21
  • 208
  • 365
1

Member functions defined in-class are inline by default.

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#note-38

gagiD
  • 36
  • 7
0

I think this depends on the C++ compiler's option. If the optimize level is high, some tiny functions will be optimized as inline.

In your case, function example2 is tiny and simple, the keyword inline will work. For example1, if the the optimize level is high, it may be compiled as inline, but it depends on compiler's behavior. If the optimize option is disabled, it will not be a inline function.

In my mind, the keyword inline is a suggestion to the compiler, if the function is complex, it may take no effect.