By definition, a built-in function has its own code included (by the compiler) in every place that it calls.
This means that the compiler must have access to the function code when it creates the compilation block of the callers. This is technically feasible, but not easy and difficult to scale. Please note: if you distribute only your header and library, this means that the compiler using your component must extract the code from the library!
If it is implemented by the compiler, this means that the code from the header is not useful to you, as well as for ordinary functions (changing the CPP file will require recompiling all the files depending on the header, even if it wasnโt.) The only advantage is to have a header, which does not contain code (which is still good).
The only solution currently available that I know of is to optimize the GCC connection time, so it doesn't meet your requirement to work with clang and icc: https://gcc.gnu.org/wiki/LinkTimeOptimization Something similar may exist for those compilers with certain options that would have to run code that depends on the compiler to support it.
Solution 1: You can save a โpure classโ without any implementation in the header and put inline functions outside the class (but still inside the header). You still have the definitions in the header, but it is clearly separate from the declaration, so this impairs readability. (This is my preferred solution personally)
Solution 2: Another way to mitigate your problem, if you really want only declarations in the header, is to separate your code from 3 files instead of 2: - an .h file containing only an interface - an .i file containing built-in functions and included with the .h file is a .cpp / .cc file containing the rest of the code
Obviously, this also has flaws, as your code is now split into two different files ...
Please tell me if you see a problem that I missed and what you see in these sentences.
Colin pitrat
source share