为什么 g++ 会生成两个具有不同名称修饰的构造函数? [重复]

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

测试用例如下:

// test.cpp
class X {
public:
    X();
};

X::X() { }

void foo() {
  X x;
}

编译它并读取目标文件中的符号,如下所示:

[root@localhost tmp]# g++ -c test.cpp

[root@localhost tmp]# readelf -s -W test.o

符号表'.symtab'包含12个条目:

Num:值大小类型绑定 Vis Ndx 名称

 0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
 1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS test.cpp
 2: 0000000000000000     0 SECTION LOCAL  DEFAULT    1 
 3: 0000000000000000     0 SECTION LOCAL  DEFAULT    3 
 4: 0000000000000000     0 SECTION LOCAL  DEFAULT    4 
 5: 0000000000000000     0 SECTION LOCAL  DEFAULT    6 
 6: 0000000000000000     0 SECTION LOCAL  DEFAULT    7 
 7: 0000000000000000     0 SECTION LOCAL  DEFAULT    5 
 8: 0000000000000000    10 FUNC    GLOBAL DEFAULT    1 _ZN1XC2Ev   => X::X()
 9: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND __gxx_personality_v0
10: 0000000000000000    10 FUNC    GLOBAL DEFAULT    1 _ZN1XC1Ev   => X::X()
11: 000000000000000a    22 FUNC    GLOBAL DEFAULT    1 _Z3foov

[root@localhost tmp]# c++filt _ZN1XC1Ev

X::X()

[root@localhost tmp]# c++filt _ZN1XC2Ev

X::X()

为什么 g++ 生成两个具有不同名称 manglings 的构造函数(

_ZN1XC1Ev
_ZN1XC2Ev
)?

c++ g++
1个回答
0
投票

这是 G++ 的一个已知缺陷。请参阅已知的 g++ bug

     G++ emits two copies of constructors and destructors.
     In general there are three types of constructors (and destructors).

     1.The complete object constructor/destructor.
     2.The base object constructor/destructor.
     3.The allocating constructor/deallocating destructor.

     The first two are different, when virtual base classes are involved. 

如果你想了解更多关于这三类ctors和dtors的信息,请参考link

© www.soinside.com 2019 - 2024. All rights reserved.