Are static member functions in C ++ copied to multiple translation units?

I have a helper class in my program that has many static functions used in different classes of my program. For example.

helper.h :

 Class helper { public: static void fn1 () { /* defined in header itself */ } /* fn2 defined in src file helper.cpp */ static void fn2(); } 

The helper has only static member functions. Thus, no helper objects are created by other modules. Helper functions are used in other modules, such as:

A.cpp

 #include "helper.h" A::foo() { helper::fn1(); helper::fn2(); } 

B.cpp

 #include "helper.h" B::foo() { helper::fn1(); helper::fn2(); } 

Does the compiler create separate copies of helper functions in A.cpp and B.cpp ? I read some early posts, and I put together the answers that such a compiler would create. But when I print the address fn1 and fn2 as printf("Address of fn1 is %p\n", &helper::fn1); and printf("Address of fn1 is %p\n", &helper::fn1); from A.cpp and B.cpp , I get the same address. Now I am confused. Can someone clarify if I missed something.

The reason I'm worried about multiple copies of helper functions (if this happens) is because we are trying to reduce the size of the executable and want to optimize it.

+7
c ++ static-members
Mar 20 2018-11-21T00:
source share
3 answers

Functions defined inside the class body are implicitly marked inline . If you take the address of the function, the compiler also creates a regular copy of the function (for the compilation unit), but the linker selects only one of these copies for inclusion in the executable file, so there is only one address.

However, the nesting process can make many copies of the function, even more than the number of compilation units. Often the increased size of code duplication is compensated by increased optimization, eliminating the passage of arguments and function calls, as well as the ability to eliminate common subexpressions, etc. Although nesting is often considered a compromise between size and speed, an increase in size is often negligible or even negative.

A function that is just declared in a class and then implemented in a single compilation unit definitely has only one copy in the executable.

+8
Mar 20 2018-11-21T00:
source share

if it is visible (for example, defined in a class declaration), then it is implicitly declared built-in by many compilers.

if attached, then yes, it can be copied in some cases, in some cases, in some cases, and partially embedded in other cases.

the One Definition (ODR) rule follows, copies found in several translations will be deleted during the join (if you did not include private external lines, then you can really get redundant exported implementations).

if you come from C: static does not create a unique copy of the function in this case - it just means that you can call the function without an instance of the class that declares it.

+2
Mar 20 2018-11-11T00:
source share

The inline static class method is no different from the inline free function. Theoretically, the ODR rule means that there is one instance of the function, but in practice, the compiler can always insert it so that in fact there is no instance of the function as such.

However, the act itself , accepting the address of the function, will force the compiler to instantiate the function, and the problem of the compilation system must ensure that ODR is respected and, therefore, ensure that you always get the same address.

0
Mar 20 '11 at 23:12
source share



All Articles