Assume you use the same template_function<T>()
in two other .cpp files (a_uses_template.cpp
and b_uses_template.cpp
), which both instanciate the template implicitly.
As I understand it, this should cause code duplication, because a_uses_template.cpp
and b_uses_template.cpp
are compiled separately, so template_function<T>()
gets instanciated twice.
However, if I change the code to use just one explicit instanciation, then the resulting executable file is even bigger, not smaller as expected.
How is this possible?
main.cpp
#include <iostream>
#include "a_uses_template.h"
#include "b_uses_template.h"
int main() {
function_a_uses_template();
function_b_uses_template();
}
a_uses_template.h
#ifndef A_USES_TEMPLATE_H_
#define A_USES_TEMPLATE_H_
void function_a_uses_template();
#endif /* A_USES_TEMPLATE_H_ */
a_uses_template.cpp
#include <iostream>
#include "template_function.h"
#include "a_uses_template.h"
void function_a_uses_template() {
std::cout << "function_a_uses_template, template_function<int>(): "
<< template_function<int>() << std::endl;
}
The following are the two variants of template_function.h /.cpp, first the one with is used together with two implicit instanciations and then the two files which contain one explicit instanciation.
template_function.h (Variant with two implicit instanciations)
#ifndef TEMPLATE_FUNCTION_H_
#define TEMPLATE_FUNCTION_H_
#include <cstddef>
template<typename T> size_t template_function() {
size_t s = sizeof(T);
// some code that the compiler won't erase during optimization
while (s != 1) {
if ((s & 1) == 0) {
s /= 2;
} else {
s = 3 * s + 1;
}
}
return s;
}
#endif /* TEMPLATE_FUNCTION_H_ */
template_function.h (Variant with one explicit instanciation)
#ifndef TEMPLATE_FUNCTION_H_
#define TEMPLATE_FUNCTION_H_
#include <cstddef>
template<typename T> size_t template_function();
#endif /* TEMPLATE_FUNCTION_H_ */
template_function.cpp (Variant with one explicit instanciation)
#include "template_function.h"
template<typename T> size_t template_function() {
size_t s = sizeof(T);
// some code that the compiler won't erase during optimization
while (s != 1) {
if ((s & 1) == 0) {
s /= 2;
} else {
s = 3 * s + 1;
}
}
return s;
}
template size_t template_function<int>(); // one explicit instanciation