c++ – When the same function is defined more than once in the template function

Question: Question:

When creating a function by specialization when using the template function in c ++, the same function is generated twice when the header in which the template function is defined is used for multiple sources. How can this be solved? Should I do it?
For example, in the following cases.

// header_temp.hpp
template <typename T>
void foo(T a) {
    cout << a << endl;
}

// source_1.cpp
#include "header_temp.hpp"
#include "header_class_alice.hpp" // class Aliceが定義されている
void hoge() {
    foo<Alice>(10);
}

// source_2.cpp
#include "header_temp.hpp"
#include "header_class_alice.hpp"
void fuga() {
    foo<Alice>(10);
}

In the above case, the function foo <Alice> is generated when compiling source_1.cpp, and the function foo<Alice> foo<Alice> generated again when compiling source_2.cpp. Now the function foo<Alice> is not defined inline, so exactly the same function is defined twice. This causes problems when linking. To solve this, either template void foo<Alice>(Alice) in one of the sources and extern void foo<Alice>(Alice); in the rest of the sources, or make the definition of template foo inline in the first place. However, I think that it is wasteful from the viewpoint of binary size to inline and define the function for each source when the function becomes large (because the exact same function is defined twice).

Since the template function itself cannot be like a shared library (although it can be specialized), I think that such a problem may occur, but if it is a program other than the method using template and extern above. How can I make sure that there is only one binary specialization function void foo<Alice>(Alice) generated by the template?

And according to this, a container such as stl's vector should generate a method such as vector<Alice>::push_back for each source when the container defined by vector<Alice> is generated by multiple sources. Is that so?

Answer: Answer:

Compiling the template (with or without specialization) translates the same function / class definition in multiple translation units.
The same variables and functions are generated in multiple object files.
If the compiler + linker supports modern , the translation result of template will be weak symbol / function.
Weak is due to link time optimization.
–When there are multiple implementations in multiple object files
――Only one of them remains in the execution format after linking.

Only when the "same variables / functions" that will be generated in different translation units really have the same content, optimization that leaves only one of them can be applied.
Therefore, there is a One Definition Rule.

So in the question

You will generate methods such as vector :: push_back for each source, is that so?

The answer to this question is Yes The same variable / function is generated in multiple object files.

How can I make it exist only one?

The answer to this question is "no need to consider" (the implementation side handles it)

I think it's wasted in terms of binary size

It's just a waste in terms of the binary size of the object file.
The binary size of the final executable file is not wasted.

Compiling non- template , that is, old-fashioned normal functions / variables, becomes STRONG and the behavior of link-time optimization changes. The STRONG symbol is
–Duplicate definition will result in an error at link time
–If the same symbol is defined in both STRONG / WEAK, STRONG will be adopted and WEAK will be ignored.

When there are both template template with the same name
It is from this area that the template ones disappear and only the non- template ones remain.

Scroll to Top