【发布时间】:2021-08-31 16:01:18
【问题描述】:
所以我试图在翻译单元之间共享一个模板化的全局变量。
对于函数执行此操作有一种通用策略,其中一个头文件声明/实现模板,第二个 C++ 文件显式枚举所有参数以实际生成链接器代码。
原版的样子
template<typename T>
struct PoolType
{
void do_something()
{
}
};
template <class T> PoolType<T> pool;
int main ()
{
pool<int>.do_something();
pool<float>.do_something();// can make other types
}
将这种方法扩展到模板结构时,我遇到了一些奇怪的错误。有谁知道错误的含义以及“声明”了哪种实体?
template<typename T>
struct PoolType
{
void do_something()
{
}
};
template <class T> extern PoolType<int> pool; //Suppoedly can live in another TU
//In some other file
PoolType<int> pool;
// template <class T> PoolType<int> pool; //works fine but limited to one TU
int main ()
{
pool<int>.do_something();
}
海合会
error: 'PoolType<int> pool' redeclared as different kind of entity
叮当
error: redefinition of 'pool' as different kind of symbol
【问题讨论】:
标签: c++ c++20 template-meta-programming linkage