1

I want to define my functions in a header file, because I want my code to be header only. The problem is, that I need to include the same header file into multiple files, which causes function redefinition. One way to solve it would be inlining all the functions, but that would make them very inefficient (They use recursion, loops, etc.). I know that any compiler would probably just ignore the inlining in this case, but I want to be 100% sure, that those inline functions won't get inlined.

Can I use something like: inline __declspec(noinline) void function(){};?

Dave F.
  • 145
  • 6
  • in a nutshell "inline" has two meanings: A) you can define the function in a header B) calls to the function will get inlined in the calling code. Due to historical reasons there is now some confusion between the two. You want to define them in the header (A) but that does not imply that calls will be inlined (B). – 463035818_is_not_an_ai Jun 02 '22 at 12:15
  • 3
    `inline` doesn't mean insert this code in line. It's just a suggestion to the compiler and if the compiler decides it would be less efficient, it won't actually inline the function. It should also be noted that if you have optimizations turned on, even non inline functions may be inlined. – NathanOliver Jun 02 '22 at 12:15
  • what happens when you try `inline __declspec(noinline) void function(){};` ? – 463035818_is_not_an_ai Jun 02 '22 at 12:15
  • What does "function redefinition" mean? Correct use of `inline` will not result in any compilation error. Please show the code that results in your compilation error, in a manner that meets all requirements for a [mre], see [ask] for more information. – Sam Varshavchik Jun 02 '22 at 12:17
  • 2
    `inline` does not mean "expand this function inline", it means "the definition of this function may appear in more than one translation unit". A modern compiler is much better at determining when actual inlining of function calls is beneficial than humans are. (Note that all member functions defined inside a class definition are `inline`.) – molbdnilo Jun 02 '22 at 12:21
  • You could simply declare your function `static` and then it will be at the compiler's discretion whether to inline. However, then you'll get a separate out-of-line copy of your function emitted for each translation unit where your header is included (unless the compiler notices that it isn't used in a particular translation unit and optimizes it out). The most desirable outcome, where you just get one out-of-line copy for the entire program, can't be achieved in a "header-only" fashion unless your compiler is pretty sophisticated (link-time optimization, etc). – Nate Eldredge Jun 02 '22 at 13:59
  • So my advice would be to rethink your requirement of having your code be "header-only". It may seem like a convenience, but it's not how C++ is intended to work, and there are definite costs. – Nate Eldredge Jun 02 '22 at 14:00
  • Hold fast until modules will be fully supported in all major compilers – MatG Jun 02 '22 at 15:43

0 Answers0