views:

389

answers:

5

C++ ISO standard says, that:
"A function defined within a class definition is an inline function."

Are there any compilers that IGNORE this rule?
(please, do not mistake inline with inlineD - my question is if there is a compiler, that wont put there that inline suggestion that it should)

+5  A: 

The standard says that all compilers can ignore inline requests, whether implicit or explicit. Whether or not they do so will nornally depend on whether the function can practically be inlined - for example recursive functions cannot be.

Edit: Just to clarify - the questioner is ignoring this, from the previous para in the standard to that he quoted from:

An implementation is not required to perform this inline substitution at the point of call

anon
barring sneaky destructor calls, tail recursive functions can be "inlined" into a loop though this is stretching the definition of inlining. :)
thank you for answer, but that doesnt answer my question
id.psycho
then make your question clearer
anon
Yes it does answer your question. There is no requirement in the standard that things must be inlined, and so there is nothing for the compiler to IGNORE. You are asking whether any compilers ignore a requirement that doesn't exist.
jalf
"A function defined within a class definition is an inline function." - I am not asking about inlineD function, I am asking about inline function (=suggestion)
id.psycho
"The standard says that all compilers can ignore inline declarations": No it does not. A compiler is required to allow multiple definitions. Any code generation changes are purely optional.
Richard
ad Edit:My question has nothing to do with 'inline substitution'. My question was, from the beginning, whether compilers FOLLOW STANDARD that I quoted.
id.psycho
+5  A: 

You seem to be misunderstanding what "inline" means. It doesn't mean functions will automatically be inlined; according to 7.1.2-2 it indicates that inline substitution is to be preferred.

Therefore, you can't tell whether a function is labeled inline or not from the code, since the compiler is free to decide one way or another. It's just a compiler hint.

David Thornley
I believe that every compiler that follows this standard would inline EMPTY function.Suppose this empty function is called millions of times in a loop - i believe that compiler SHOULD inline it, because efficiency would be 1000000*call_time / 0*inlined_nothing.
id.psycho
you can believe what you like - it doesn't make it true
anon
Actual inlining is a quality of implementation issue. The compiler is perfectly free to inline or not. If you don't like it, see if the options change things, or get another compiler.
David Thornley
A: 

Compiler's usually inline based on the number of calls to the function, the number of pseudo-instructions in the function, and a bunch of other things. Take a look at the GCC documentation on optimization options for an idea of how it does things. Basically, the inline keyword is just a hint that bumps up the likelihood that the compiler will inline. The actual decision to inline is usually complex.

D.Shawley
+3  A: 

I suspect your test is flawed. You can't test with only one such file whether the compiler ignores the inline specifier or not.

You need to include the header containing the inline function definition and include it into multiple implementation files that are then linked together. If you get linker errors about multiple defined instances of that functions, then the compiler is ignoring the inline specifier regarding its most important property: Allowing it to be defined multiple times across the entire program while still retaining the same address for it and its local static variables.

What your test probably checks is whether or not the compiler inlines the call to the function, which is actually only a hint to the compiler and only a small of many other more important consequences of the inline specifier. If the compiler does not not inline a call to the function, it is fine doing so. The standard does not require it to do anything in this matter.

Johannes Schaub - litb
+1  A: 

See my answer to a very similar question: http://stackoverflow.com/questions/582919/when-is-inline-ineffective-in-c/582932#582932

Summary: inline is only required to allow multiple definitions. Any function calling changes is purely optional.

Richard
note that that other answer is about C. in C++ it is different: it does not require "exactly one external definition" of a function if you use an inline function: the inline function itself is that definition in C++. in C, it isn't, but it is only an "inline definition".
Johannes Schaub - litb
I know, but I actually re-read the relevant terms for C++ there is little practical difference for this question: which is all about failing to understand what inline really means.
Richard