函数模板化时链接器错误未找到析构函数

时间:2017-05-21 00:57:40

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

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

#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()中删除模板参数,它将进行编译。

请注意,示例过于简单,实际的软件需要自定义构造函数,模板参数等。

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

1 个答案:

答案 0 :(得分:0)

确认clang中的错误&lt; 4似乎。