不同版本的破坏者称为奇怪因素

Different versions of destructor called depending on strange factors

本文关键字:版本 破坏者      更新时间:2023-10-16

为什么以下代码生成这样的输出?

main.cpp ctor 0x24a4c30
test.cpp dtor 0x24a4c30

test.cpp

#include <boost/optional.hpp>
struct Test
{
    Test()
    {
        printf("test.cpp ctor %pn", (void *) this);
    }
    ~Test()
    {
        printf("test.cpp dtor %pn", (void *) this);
    }
};
boost::optional< Test > t;

main.cpp

#include <memory>
struct Test
{
    Test()
    {
        printf("main.cpp ctor %pn", (void *) this);
    }
    ~Test()
    {
        printf("main.cpp dtor %pn", (void *) this);
    }
};
int
main(void)
{
    std::make_shared< Test >();
    return 0;
}

编译
g++ -std=c++11 -c test.cpp -o test.o
g++ -std=c++11 -c main.cpp -o main.o
g++ -std=c++11 test.o main.o

我解释了这种行为,即 test.o 提供test的 ctor&amp;然后,然后从 main.o 中丢弃重复的符号,但仅对 dtor 是正确的。如果我删除静态对象 t ,则会从 test.o 中丢弃符号,并且输出是下一个

main.cpp ctor 0x208ec30
main.cpp dtor 0x208ec30

如评论中所述,在两个链接的翻译单元中定义相同的符号违反了一个定义规则,而

只要每个程序都可能有一个以上的定义 定义出现在不同的翻译单元中 以下:类Type ,枚举类型,内联功能与外部功能 linkage ,外部链接的内联变量(自C 17(,类 模板,非静态功能模板,类的静态数据成员 模板,类模板的成员功能,部分模板 专业,概念(自C 20(,只要以下所有内容 是真的:

  • 每个定义都由同一序列组成(通常,出现在同一标头文件中(
  • [...]

如果满足了所有这些要求,则该程序的行为好像 整个程序中只有一个定义。否则, 行为不确定(重点是我的(

观察到UB的结果不需要解释,也可能没有任何解释。

当然,这是公然的违规行为。但是,在外观更无辜的程序中可能会出现非常相似的情况(并导致奇怪而难以找到错误(。例如,当某些类声明更改时,针对较旧的单独编译对象链接。