Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are inline function static constants unique?

Tags:

c++

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:

  1. Does the C++ standard guarantee that NAMES will exists in only one object file, or is it left to the compiler to decide or does it guarantee that every object file will have it's copy?
  2. If there will be multiple copies, will it be a linking problem (I'm assuming I can't test enough compilers to check that).
  3. Will gcc, msvc and clang all optimize out this case by making the final binary have only one instance of NAMES?
like image 711
Klaim Avatar asked Nov 08 '12 23:11

Klaim


People also ask

Can inline functions have static variables?

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.

Does inline imply static?

No, inline does not imply static not vice versa.

What are disadvantages of inline?

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.

What is the main advantage of inline function?

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.


2 Answers

Yes, the standard guarantees that there will be only one object. From C++03 §7.1.2/4:

[...] A static local variable in an extern 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.

like image 84
Adam Rosenfield Avatar answered Sep 20 '22 15:09

Adam Rosenfield


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.

like image 45
Mark Ransom Avatar answered Sep 17 '22 15:09

Mark Ransom