模板化函数时链接器错误未找到析构函数
Linker error not finding destructor when a function is templated
我有一个奇怪的错误,我无法解决这个问题。我已将其简化为一个简单的示例。我可以通过创建一个空的析构函数来修复它,但我真的很想知道发生了什么。
#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 中的已确认错误似乎 < 4。
我有一个奇怪的错误,我无法解决这个问题。我已将其简化为一个简单的示例。我可以通过创建一个空的析构函数来修复它,但我真的很想知道发生了什么。
#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 中的已确认错误似乎 < 4。