Reputation: 16747
Is it possible to force compiler (CLang/GCC/MSVC) to throw compile error whenever forced-inline function failed to be inlined for any reason?
Imagine next code:
Following code in online compiler!
#include <iostream>
inline __attribute__((always_inline)) int f(int i) {
if (i <= 0)
return 0;
return f(i - 1) + f(i - 2) + i;
};
int main() {
std::cout << f(10) << std::endl;
}
Because of two recursive self-calls function f() failed to be inlined although was instructed to be inlined always by __attribute__((always_inline))
. You can see in assembler by link above that there is instruction call f(int)
, meaning that function was not inlined.
Is it possible anyhow for the code above to throw compile error (in CLang/MSVC/GCC) because it failed to inline? Maybe some compile-time static_assert(...)
expression can be used for that?
I expect that there can be other reasons for non-inlining, both due properties of function code and due to properties of code on caller side. So in general probably you can't be sure in advance that your function is inlined in 100% of all calls. Hence solution to my question will be useful.
Also interested in solving same question for the case of lambda-functions inlining.
Upvotes: 1
Views: 538
Reputation: 610
you can set a warning level in visual studio to fail compilation on all warnings. There is no Assertion to check for inline failure.
A better answer here
You can use -Winline in GCC and switch on specific flag inorder to consider warning into error.
'-Werror= Make the specified warning into an errors. The specifier for a warning is appended, for example -Werror=switch turns the warnings controlled by -Wswitch into errors. This switch takes a negative form, to be used to negate -Werror for specific warnings, for example -Wno-error=switch makes -Wswitch warnings not be errors, even when -Werror is in effect. You can use the -fdiagnostics-show-option option to have each controllable warning amended with the option which controls it, to determine what to use with this option.'
Upvotes: 2