Search code examples
c++cinline

Is declaring the function and then defining it inline legal in C/C++?


Is the following legal in C and/or C++?

void fn();

inline void fn()
{
  /*Do something here*/
}

What concerns me is that the first declaration looks like it implies that the function will be defined non-inline, but the following definition turns it into an inline after all.

If there's a difference between C and C++ in this case, it would be nice to know that difference, too.


Solution

  • Yes. In fact, it's probably preferable. Source: http://www.parashift.com/c++-faq/inline-nonmember-fns.html

    Rule of thumb: Declaration tells you how to call a function and use its return value. Definition says what happens when you call it. You can change a function to inline and back without modifying how you call it, so it makes sense that it's part of the definition. (Of course, what makes sense and what is true don't always match, so this is only a rule.)