A noinline inline function? What sorcery is this?

0
0
A noinline inline function? What sorcery is this?
Raymond Chen

Raymond

You’d characterize a noinline inline characteristic.

void g();

// gcc
__attribute__((noinline)) inline void f()
{
    g();
}

// MSVC
__declspec(noinline) inline void f()
{
    g();
}

void tryme()
{
    f();
    f();
}

What sorcery is that this, a characteristic that’s each inline and never-inline?

The two key phrases often at the moment are not contradictory attributable to they characterize diversified senses of the phrase “inline”.

The C++ language key phrase inline methodology “might possibly nicely be outlined in additional than one translation models with out triggering an ODR violation.” In different phrases, it allows you to place the characteristic definition in a header file that’s included by additional than one C++ information.

The characteristic attribute/declaration specifier noinline methodology “type not inline this feature precise via code interval.” It’s miles a directive to the optimizer now to not internet inline substitution precise via code interval.

Traditionally, the inline C++ key phrase grew to become as quickly as on the origin an optimizer trace, nonetheless optimizers acquired permission to disregard it and procure their very have decisions about inline substitution precise via code interval. Proper this second time, compilers fairly crucial ignore the optimization facet of the inline key phrase. The best enlighten that stays of the inline key phrase is the talent to take care of additional than one definitions with out violating ODR.

That that it’s almost definitely you will nicely snarl that the trendy sense of the C++ key phrase inline is “outlined sincere appropriate proper right here.” It’s a statement regarding the supply code, not the thing code.

Within the above occasion, the characteristic f is a noinline inline characteristic. The inline key phrase lets within the definition of f to lope right into a header file that’s consumed by additional than one translation models. The noinline attribute/declaration specifier tells the optimizer to emit code for f and phone it, in assign of embedding the physique of f into its title websites. The characteristic tryme will title the characteristic f twice, as an alternate of optimizing out the selection and regular calling g twice.

LEAVE A REPLY

Please enter your comment!
Please enter your name here