0

I read that inline is a hint at best, and a compiler may or may not inline a member function that has been declared inline, and it may also inline functions not delcared as inline at compile or link time.

If this is true, is there any difference between these two:

in "myClass.h":

class cls {
public:
  result_t fc(typeA a, typeB b);
  result_t fc(typeC c, typeB b);
  ...
}
inline result_t cls::fc(typeC c, typeB b) {
    return fc(c.getA(), b);
}

and

class cls {
public:
  result_t cls::fc(typeA a, typeB b);
  result_t cls::fc(typeC a, typeB b) { return fc(c.getA(), b); } // note: not 'inline'
}

Edit: Thanks to @EmileVrijdags for the excellent link, which clearifies (for me) why the definition inside the declaration MUST be inline by default: it if wasn't there would be multiple definitions (namely on each and every place where the header file is included)

Tommylee2k
  • 2,613
  • 1
  • 8
  • 22
  • 5
    `inline` in C++ became completely misleading, it has nothing to do with inlining anymore, it makes linker select one of multiple symbols of the same name from different translation units without complaining. – user7860670 Jun 14 '19 at 11:48
  • Not completely, it still influences the inlining behavior of the compiler, as I understand there is some point assigned to each function, when those points are below a threshold, the compiler will inline the function. The inline keyword changes the threshold value to some higher value. Jason Turner shows this in the following youtube video: C++ Weekly - Ep 136 - How `inline` Might Affect The Optimizer - https://youtu.be/GldFtXZkgYo. – Emile Vrijdags Jun 14 '19 at 12:23
  • 1
    In your case there is no difference between the 2 pieces of code, when a function is defined inside a class, it is 'inline', e.g. see: https://isocpp.org/wiki/faq/inline-functions , or https://en.cppreference.com/w/cpp/language/inline – Emile Vrijdags Jun 14 '19 at 12:31

0 Answers0