Search code examples
c++templates

Does compiler always use built-in type template params to decorate function name


When generating the function name, will the compiler always add template params into that name to make sure that the function name is unique? I am especially interested in the case where the template param is a built in type. Here is an example with some pseudocode:

template class<int32_t Size>
class Foo
{
public:
    ...

    // Signature does not depend on size
    void doSomething()
    {
        m_bar.doWork(Size);
    }
    
    // Some functions that depend on the value of Size
    ...

private:
    // Bar does not depend on Size
    Bar m_bar;

    // Some members that depend on the value of Size
    ...
};

Will instantiating the class above for two different values of Size result in two unique names for doSomething?


Solution

  • Possibly. Not necessarily.

    The C++ standard does not mandate how implementations do their thing. While it has certain minor facilities that at least hint at the use of a thing called "name mangling", it certainly isn't necessary. You don't need a "linker", let alone a linker which works based on "symbol names", to make a standard-compliant C++ compiler.

    What the C++ standard does mandate is that Foo<4>::doSomething() invokes Bar::doWork() with the argument 4, and that Foo<2>::doSomething() invokes Bar::doWork() with the argument 2. So it would not be okay for a compiler to confuse the two.