Search code examples
c++prototypeinline-functions

C++ Inline Function Prototypes


I came across a header file that includes various function prototype declarations which are inline and const:

inline bool Foo1() const;
inline bool Foo2() const;
inline bool Foo3() const;
...

I understand that the inline keyword allows for the compiler to (potentially) expand the function when it is called, but why not include the body of the function?

It'd make more sense to me if the definition was included in the header file:

inline bool Foo1() const { return m_Foo1; };
inline bool Foo2() const { return m_Foo2; };
inline bool Foo3() const { return m_Foo3; };
...

What is the point of using inline on the prototype?


Solution

  • It's possible that it was just a mistake, but most likely the programmer wanted to make the function inline but didn't want to clutter that portion of the file with the implementations of these functions. This is a fairly common pattern when writing "header only" code. You have a normal header file with no (or few) implementations and another file that is included but acts like an implementation file and contains all the implementations.