Movatterモバイル変換


[0]ホーム

URL:


This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 119a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2025-12-20


317. Can a function be declared inline after it has been called?

Section:9.2.3  [dcl.fct.spec]    Status:CD1    Submitter:Steve Clamage    Date:14 Oct 2001

[Voted into WP at October 2005 meeting.]

Steve Clamage:Consider this sequence of declarations:

  void foo() { ... }  inline void foo();
The non-inline definition of foo precedes the inline declaration.It seems to me this code should be ill-formed, but I could not findanything in the standard to cover the situation.

Bjarne Stroustrup:Neither could I, so I looked in the ARM, which addressed this case (apparentlyfor member function only) in some detail in 7.1.2 (pp103-104).

The ARM allows declaring a function inline after its initial declaration,as long as it has not been called.

Steve Clamage:If the above code is valid, how about this:

  void foo() { ... }    // define foo  void bar() { foo(); } // use foo  inline void foo();    // declare foo inline

Bjarne Stroustrup:... and [the ARM] disallows declaring a function inline after ithas been called.

This may still be a good resolution.

Steve Clamage:But the situation in the ARM is the reverse: Declare a functioninline, and define it later (with no intervening call). That'sa long-standing rule in C++, and allows you to write memberfunction definitions outside the class.

In my example, the compiler could reasonably process the entirefunction as out-of-line, and not discover the inline declarationuntil it was too late to save the information necessary for inlinegeneration. The equivalent of another compiler pass would be neededto handle this situation.

Bjarne Stroustrup:I see, and I think your argument it conclusive.

Steve Clamage:I'd like to open a core issue on this point, and I recommend wordingalong the lines of: "A function defined without an inline specifiershall not be followed by a declaration having an inline specifier."

I'd still like to allow the common idiom

  class T {    int f();  };  inline int T::f() { ... }

Martin Sebor:Since the inline keyword is just a hint to the compiler, I don't seeany harm in allowing the construct. Your hypothetical compiler cansimply ignore the inline on the second declaration. On the other hand,I feel that adding another special rule will unnecessarily complicatethe language.

Steve Clamage:The inline specifier is more than a hint. You can have multipledefinitions of inline functions, but only one definition of afunction not declared inline. In particular, suppose the aboveexample were in a header file, and included multiple times ina program.

Proposed resolution (October, 2004):

Add the indicated words to 9.2.3 [dcl.fct.spec] paragraph 4:

An inline function shall be defined in every translation unit inwhich it is used and shall have exactly the same definition inevery case (6.3 [basic.def.odr]). [Note: a call tothe inline function may be encountered before its definitionappears in the translation unit. —end note]If thedefinition of a function appears in a translation unit before itsfirst declaration as inline, the program is ill-formed. If afunction with external linkage is declared inline in onetranslation unit...



[8]ページ先頭

©2009-2026 Movatter.jp