I know that by default member functions of a class are inlined

class d {
public:
int a() {
return 1545435;
}
};

And I also know that it's not allowed to declare functions in headers unless they are inlined.

Is there a way to declare a function in a header's class that is not inlined?
For example
file.h

class d {
public:
int a();
};

int d::a() {
do
a
lot 
of
stuff
lots
of
code
...
}

Will it still be inlined?


And is there a keyword to explicitly specify to make any function not-inlined? (I know that by default normal functions are not inlined, but still!)

Recommended Answers

All 2 Replies

>>And I also know that it's not allowed to declare functions in headers unless they are inlined

Wrong -- see your second example.

>>Will it still be inlined?
No (assuming lines 6-16 are in a *.cpp file and not *.h; there will be lots of other problems if you put that code in a *.h file).

>>And is there a keyword to explicitly specify to make any function not-inlined?
No. The default is not inlined. A method is only inlined if you explicitly ask it to be with the inline keyword or by coding it as in your first example. Even then there is no guarantee that the compiler will inline the code, inline keyword is only a HINT to the compiler, not a requirement.

commented: thanks +7

>>I know that by default member functions of a class are inlined

If member functions are defined within the class declaration, then yes, it is implicitly assumed to have been declared with the inline keyword. Whether it will be inlined by the compiler is another question that only compiler-vendors can answer.

>>And I also know that it's not allowed to declare functions in headers unless they are inlined.

It is certainly allowed to _define_ the member functions in the headers, but it can generally cause linkage problems when that header is included within different compilation units (e.g. compiled cpp files), because the linker will find multiple definitions of the function in question. In order to solve that problem, you have to change the default linking mechanism of the function, that is, with the inline or static keyword (I always have to repeat to people that the main effect of inline is on the linkage mechanism not on whether or not a function gets inlined by the compiler).

>>Is there a way to declare a function in a header's class that is not inlined?

Yes. If you want the function to be defined in the header file, you can specify it with the static keyword giving it static-linkage. This means that the function definition will not be published on the symbol tables that are used by the linker, effectively making it invisible to any code outside the current compilation unit (i.e. formally "static-linkage" means that its linkage is restricted to the compilation phase (which does not include the linking phase)).

The primary effect of the inline keyword is to specify that "internal linkage" be used for this function (note that static-linkage is also internal linkage but more strictly so). On most compilers internal-linkage can be understood as an exception to the one-definition-rule of C++, an exception that could be stated as: "Prefer to link to the internal definition of a function (internal to the compilation unit), if not found, link to an external one at link-time. If multiple definitions are found by the linker, they can safely be assumed to all be the same without breaking the ODR.". This is the required default linkage rule that applies to templates in C++ (functions or classes), and it is probably why most compilers apply that rule to inline functions too. Additionally, the inline keyword tells the compiler to also calculate if it is useful to do an inline-expansion of the function's code instead of a normal link (function call).

>>Will it still be inlined?

Ask your compiler-vendor. Besides putting the function definition in a separated compilation unit (cpp file) from all the code that calls it, there is, as far as I know, no other way to guarantee that the compiler won't inline the function (and even then, some globally-optimizing compilers might still inline functions whose definition and calls appear in separate compilation units). My understanding of this issue is that whether a function is declared inline or not (equivalently, defined in the class declaration or not), the compiler can decide to inline the function in some calling contexts where it appears reasonable to the compiler. However, it will not waste time analysing the advantages of inlining the function unless it has good reasons to think it might be advantageous, and, amoungst those reasons there are at least two: if the function definition is really short, or if it is declared inline (or equivalently, defined within the class declaration). In other words, besides the linkage specification, the inline keyword tells the compiler "it is worth investigating the advantages of inlining this function", but the compiler could decide that it is worth doing so for any other functions as well.

In any cases, function inlining is an optimization that follows the as-if rule, meaning that the compiler will never do inlining in a way that would change the behaviour of the program in any way, rest assured. What does affect the behaviour of the program in the context of using the inline keyword is the linkage mechanism, and that is why I keep stressing that this is the most important aspect to consider, not whether function calls get replaced by inline expansions (that's a trivial matter for the compiler to decide and it has, or should have, no visible effect on the program's behaviour (except for performance gains)).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.