Search code examples
c++classtemplates

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


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.


Solution

  • 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.