1

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)

pad
  • 41,040
  • 7
  • 92
  • 166
id.psycho
  • 21
  • 3

5 Answers5

8

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
  • 56,304
  • 9
  • 91
  • 158
  • 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 Mar 17 '09 at 15:57
  • you can believe what you like - it doesn't make it true –  Mar 17 '09 at 16:15
  • 1
    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 Mar 17 '09 at 17:41
  • If you wrote a C++ compiler which never inlined any function under any circumstance, it would still be completely standards compliant in this regard. – Ferruccio Jan 02 '12 at 19:58
5

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

  • barring sneaky destructor calls, tail recursive functions can be "inlined" into a loop though this is stretching the definition of inlining. :) –  Mar 17 '09 at 14:55
  • 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 Mar 17 '09 at 16:23
  • "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 Mar 17 '09 at 16:26
  • 1
    "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 Mar 17 '09 at 16:27
  • 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 Mar 17 '09 at 17:27
4

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
  • 496,577
  • 130
  • 894
  • 1,212
2

See my answer to a very similar question: When is "inline" ineffective? (in C)

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

Community
  • 1
  • 1
Richard
  • 106,783
  • 21
  • 203
  • 265
  • 1
    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 Mar 17 '09 at 16:47
  • 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 Mar 17 '09 at 16:54
0

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
  • 58,213
  • 10
  • 98
  • 113