【问题标题】:Linker error not finding destructor when a function is templated模板化函数时链接器错误未找到析构函数
【发布时间】:2017-10-20 21:23:58
【问题描述】:

我有一个奇怪的错误,我无法解决这个问题。我把它简化为一个简单的例子。我可以通过创建一个空的析构函数来修复它,但我真的很想知道发生了什么。

#include <cstdio>
#include <functional>

struct test {
    inline test()
    {}

    const std::function<void()> f;
    const int universe = 42;
};

template<size_t n = 1>
inline void do_test(const test& t = {}) {
    printf("%d\n", t.universe);
}

int main(int, char**) {
//  test t;
    do_test();
    return 0;
}

这不会编译,输出错误:

clang++ -O3 -std=c++1z -stdlib=libc++ -Wall main.cpp
Undefined symbols for architecture x86_64:
  "test::~test()", referenced from:
      _main in main-df96d9.o
ld: symbol(s) not found for architecture x86_64

如果您选择取消注释我创建test t; 对象的行,或者从do_test() 中删除模板参数,它将编译。

注意例子比较简单,实际软件需要自定义构造函数、模板参数等。

知道为什么它抱怨找不到析构函数吗?

【问题讨论】:

  • 看起来像一个 Clang 错误。 GCC 对此代码没有问题。
  • 在 VS2015 中编译,这里看起来还可以。那么它必须是特定于c的。尝试使用“test()”而不是“{}”初始化“const test&”,看看它是否是与初始化列表相关的特定错误。可能发生的情况是编译器没有注意到它需要为该类创建一个默认析构函数,而您只通过引用引用了该类,并且初始化程序列表 => 构造函数自动转换仅在它启动时才启动解决了类的细节。

标签: c++ c++11 linker clang linker-errors


【解决方案1】:

确认了 clang 中的错误

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多