Suppose I have a very simple function that I expect the compiler to inline it. But I may need to throw exception on seeing some invalid input, would that stop the compiler from inlining the function?
Would C++ exception stops function from being inlined?
A compiler can refuse to inline for any reason. gcc lists reasons why it might not inline a function, but exception throwing is not among them. Also, the option -Winline will cause the compiler to issue a warning if it can't inline a function that you marked as inline. You can try that and see if you are doing anything to prevent inlining.
It is perfectly reasonable and valid for a compiler to inline a function that throws.
I've just run into this situation with MSVC version 19. A function that throws an exception would not get inlined when compiling for x86. If I replace throw
with exit(1)
or if I compile for x86-64, it gets inlined just fine.
The behaviour not to inline a function with a try statement seems to be documented here: learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/… –
Ascender
© 2022 - 2024 — McMap. All rights reserved.
... Inlining of specified subprogram failed due to the presence of a C++ exception handler: utills::ScopeFile<utills::MpiAbbortOnError>::~ScopeFile()
. Notice that the thing that it complains about is not even an exception, but it seems the destructors have to be treated in the same manner. These warnings appear during the linking phase, when I doxlC object1.o object2.o -o my_binary
(xlC is the IBM compiler). – Blossomblot