Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is non-template member function in class template generated only once in executable? [duplicate]

Example:

template<typename T>
class A {
    void f() { std::cout << "f";}
};

...

A<int> a;
A<double> a;

Is f generated once or multiple times (once for each template instantiation) in a final binary after compilation and linking? I can't seem to find this rule in the standard. AI says it's generated only once, but I don't trust it enough.

like image 973
toxic Avatar asked Feb 19 '26 19:02

toxic


1 Answers

The standard doesn't describe the structure of a program "in a final binary after compilation and linking".

There can be any number of copies of instructions that correspond to outputting the character f to standard output. There could be 0, 1, or 2 symbols exported corresponding to those functions.

What is required is that the pointer values &A<int>::f and &A<double>::f compare unequal.

like image 72
Caleth Avatar answered Feb 22 '26 08:02

Caleth