Andres Tiraboschi
Andres Tiraboschi

Reputation: 543

Functions not being inlined with g++

I'm compiling the next simple example:

#include <iostream>

struct PP
{
    inline void wInline();
}

inline void PP::wInline()
{
    std::cout << "hola" << endl;
}

int main()
{
    PP pp;
    pp.wInline();
    return 0;
}

in this way:

g++ -O0 -finline-functions -finline-functions-called-once
-finline-small-functions -Wall -Wextra -pedantic -std=c++11 -Winline
main.cpp

with gcc 4.8.2 and wInline is not being inlined.

Are the -finline-functions, -finline-functions-called-once and -finline-small-functions flags enough? Why am I having no warnings even with the -Winline flag enabled?

Upvotes: 1

Views: 279

Answers (1)

Khouri Giordano
Khouri Giordano

Reputation: 1461

The inline keyword is a hint to the optimizer. Since you are compiling with -O0, no optimization is done.

The gcc documentation says this about -Winline:

Warn if a function that is declared as inline cannot be inlined.

There are rules for what functions can be declared inline as well as heuristics that gcc uses to decide if it should inline a function declared as inline.

You can read more about the inline keyword here.

(Edited to answer second question.)

Upvotes: 1

Related Questions