列举所有模板的实例化

Enumerating all instantiations of template

本文关键字:实例化      更新时间:2023-10-16

我正在阅读类型擦除:

Andrzej的C 博客类型擦除 - Part I

我遇到以下文本的位置:

除非您可以列举模板的所有实例化 提前,您必须将每个功能模板的主体包括在 标题文件,您无法将声明与 实施

列举模板的所有实例化,就像回答以下问题时指出的显式实例化吗?

为什么仅在标题文件中实现模板?

另一个解决方案是使实施分开,并且 明确实例化您需要的所有模板实例:

// Foo.h
// no implementation
template <typename T> struct Foo { ... };
//----------------------------------------    
// Foo.cpp
// implementation of Foo's methods
// explicit instantiations
template class Foo<int>;
template class Foo<float>;
// You will only be able to use Foo with int or float

主要是是。

它归结为"您能知道到处使用吗?"。std::vector<T> 无法的作者知道所有类型T都将被替换。

这是"列举所有实例"步骤,其次是"写下所有明确的实例"。