inline function and class and header file

c++, class, inline, one-definition-rule

Solution

Any function defined inside a class definition is inline. Any function marked `inline` is also inline.

class C {
    int f() { return 3; } // inline
    int g();
    int h();
}


inline int C::g() { return 4; } // inline
int C::h() { return 5; } // not inline

If all this code is in a header and that header is used in more than one translation unit you'll get a complaint that `C::h` has more than one definition. `C::f` and `C::g` are okay because they're inline. That's the primary role of `inline` these days: to permit defining the same function in multiple places (provided the definitions are "the same").

Problem

- Will any function defined in the header file automatically be inline? - If I declare a function in a class and give the definition outside using keyword inline, will this function be inline? If it is, why this does not against the law that inline function should be given the body at declaration?

Original source

Related problems