编译器是否总是使用内置类型模板参数来修饰函数名称

问题描述 投票:0回答:1

在生成函数名称时,编译器是否总是在该名称中添加模板参数以确保函数名称是唯一的?我对模板参数是内置类型的情况特别感兴趣。这是一个带有一些伪代码的示例:

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
    ...
};

Size
的两个不同值实例化上面的类是否会导致
doSomething
有两个唯一的名称?

c++ compiler-construction
1个回答
0
投票

是的,这就是 Mac 上 g++ 的样子,其中 main 使用两个不同的参数实例化 Foo:

int main(int argc, char* argv[]) {
    Foo<5>().doSomething();
    Foo<7>().doSomething();
    return 0;
}

这里是

nm a.out
的部分输出,其中列出了所有已定义的符号 - 一个函数用于
Foo<5>
,一个函数用于
Foo<7>

...
0000000100003f14 T __ZN3FooILi5EE11doSomethingEv
0000000100003f3c T __ZN3FooILi7EE11doSomethingEv
...
© www.soinside.com 2019 - 2024. All rights reserved.