Wal*_*ter 8 c++ templates explicit-instantiation c++11 template-instantiation
以下代码(我无法制作更短的MVCE)
unit.h:
#include <vector>
template<typename T>
struct foo
{
std::vector<T> data;
foo(foo&&) = default; // no assembly generated
foo(std::vector<T>&&v) : data(std::move(v)) {}
};
extern template struct foo<int>; // indicates template instantiation elsewhere
Run Code Online (Sandbox Code Playgroud)
unit.cc:
#include "unit.h"
template struct foo<int>; // forces template intantiation
Run Code Online (Sandbox Code Playgroud)
main.cc:
#include "unit.h"
struct bar
{
foo<int> f;
bar(foo<int>&&x) : f(std::move(x)) {}
};
bar makeBar(int x)
{
std::vector<int> v(x);
foo<int> f(std::move(v));
return {std::move(f)};
}
int main()
{
bar x = makeBar(5);
}
Run Code Online (Sandbox Code Playgroud)
不履行铛编译(苹果LLVM版本9.0.0(铛- 900.0.39.2) - 这LLVM是那个版本?),结果:
test> clang++ -std=c++11 -c unit.cc
test> clang++ -std=c++11 -c main.cc
test> clang++ -std=c++11 main.o unit.o
Undefined symbols for architecture x86_64:
"foo<int>::foo(foo<int>&&)", referenced from:
bar::bar(foo<int>&&) in main-476e7b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)
使用gcc(8.2.0)一切正常.在检查时,似乎gcc会foo<int>::foo(foo<int>&&)进入main.o,而clang无法完全发出它.
什么是正确的行为:default移动构造函数应该发出unit.o或main.o?这是一个已知的clang bug吗?
有用的链接:https://en.cppreference.com/w/cpp/language/class_template
这是一个铿锵的错误.您的代码格式正确,因此无论编译器的策略是什么,考虑到"似乎"规则,您的代码都应该编译.
类模板的显式实例化仅实例化为其提供定义的成员[temp.explicit]/9:
命名类模板特化的显式实例化定义显式实例化类模板特化,并且是仅在实例化时定义的那些成员的显式实例化定义.
特殊成员函数默认的第一个声明仅在odr-used时定义.所以我想,这个错误是锵预计,在显式实例化的角度来看,在默认的构造函数也初始化.
所以周围的工作可能是第一个,申报在头文件中的移动构造函数,然后将其定义为在实现文件默认:
unit.hpp:
template<typename T>
struct foo
{
std::vector<T> data;
foo(foo&&)=default;
foo(std::vector<T>&&v) : data(std::move(v)) {}
};
template<T>
foo<T>::foo(foo&&) noexcept;
extern template struct foo<int>;
Run Code Online (Sandbox Code Playgroud)
unit.cpp:
#include <unit.hpp>
template<T>
foo<T>::foo(foo&&) noexcept = default;
template struct foo<int>; //foo(foo&&) has a definition so it is instantiated with the class.
Run Code Online (Sandbox Code Playgroud)
这将强制生成默认移动构造函数的定义(参见[dlc.fct.def.default]/5).缺点是,定义foo(foo&&)不再是内联的.
或者,下面的解决方案将起作用:
template<typename T>
struct foo
{
std::vector<T> data;
foo(foo&& o)noexcept:data{move(o.data)}{};
foo(std::vector<T>&&v) : data(std::move(v)) {}
};
Run Code Online (Sandbox Code Playgroud)