Here is an example code:
enum Foo // or enum class whatever
{ BAR
, STUFF
};
inline const char* to_string( const Foo& foo )
{
static const char* const NAMES[] =
{ "BAR"
, "STUFF"
};
// let's assume I have some boundary checks here, it's not the point
return NAMES[foo];
};
This function is inline, is in a header used in several compilation units. The goal here is to make the compiler do nothing if there is no use of this function.
Questions:
Static local variables are not allowed to be defined within the body of an inline function. C++ functions implemented inside of a class declaration are automatically defined inline.
No, inline does not imply static not vice versa.
5) Inline functions may not be useful for many embedded systems. Because in embedded systems code size is more important than speed. 6) Inline functions might cause thrashing because inlining might increase size of the binary executable file. Thrashing in memory causes performance of computer to degrade.
Inline Function in C++ The main advantage of inline functions is that you can use them with C++ classes as well. When an instruction of a function call is encountered during the compilation of a program, its memory address is stored by the compiler.
Yes, the standard guarantees that there will be only one object. From C++03 §7.1.2/4:
[...] A
static
local variable in anextern inline
function always refers to the same object. A string literal in an extern inline function is the same object in different translation units.
(Note that an extern inline
function is an inline
function with external linkage, i.e. an inline
function not marked as static
.)
Exactly which object file it appears in will depend on the compiler, but what I suspect happens is that each object file that uses it will get a copy, and the linker will arbitrarily choose one of the symbols and discard the rest.
The standard guarantees that only one copy will be used. It doesn't guarantee that there won't be unused copies taking up space in the code.
The linker is generally responsible for consolidating all the references to use the same instance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With