Should I repeat the inlined keyword in the function implementation

I always try to keep the implementation outside of the headers, so for templates and inline functions, I usually do something like this


// File.h inline bool foo() #include "File.hpp" 

 // File.hpp inline bool foo() { return 1; } 

My question is: what does the C ++ specification have to say about repeating the inline keyword for the actual implementation of the function? (as shown in this example)

I really don't want to do this because it becomes messy with lots and lots of functions, and although my compiler doesn't complain, I'm wondering if the compiler is not compiling an inline hint.

Somebody knows?

+6
source share
2 answers

I try to put the inline as far as possible from the interface, as this is an implementation detail, not part of the interface. Therefore: omit the first inline in the declaration. And attach it only to the function definition. To include compiler areas, hpp is not relevant to inline because files are considered as concatenated. See also http://www.parashift.com/c++-faq/where-to-put-inline-keyword.html for a more detailed explanation.

+4
source

This is fine, but placing inline in the source file is even less hinted because the sources are usually not visible to other translation units. If you implement a function outside the header, the compiler probably will not be able to inline it.

The only practical use of inline , in my opinion, is to prevent multiple definitions of the functions defined in the header.

+4
source

Source: https://habr.com/ru/post/920684/


All Articles