4

A C++ question about struct member functions.

What is the difference between f1() and f2() except their names ?

 struct F{
   int f1(){
      return 0;  
    }
    int f2();    
 }; 
 int F::f2(){
     return 0;
 }

May I say f1() is inline but f2() is not ?

2
  • 2
    That depends on what you mean by inline, but yes, in the context of how it's defined that's fine.
    – Carl Norum
    Sep 11, 2013 at 23:26
  • It is inline according to definition, but the compiler reserves the right to inline it or not (don't count on that it will).
    – imreal
    Sep 11, 2013 at 23:44

1 Answer 1

11

You are correct that f1 is inline and f2 is not, but its not just because it was defined inside the class. f2 could also be inline if it was defined as

inline int F::f2() {
    return 0;
}

The C++11 spec section 9.3 says that f1 is "defined in its class definition" and f2 is "defined outside of its class definition." It then states that any function defined inside its class definition is inline, while functions defined outside of its class definition must be explicitly marked as inline (like in my example above), or else they are non-inline (like your f2).

Inside the class definition vs. outside the definition is unimportant, other than making functions implicitly inline. The concept of inside a class definition and outside a class definition only appears in 9.3.2-9.3.5, while the broader concept of "inline" appears in other parts of the spec.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.