是否有理由在标题中保留完全专用的模板?
Are there reasons for keeping fully specialized template in the header?
如果我有一个完全专门的模板函数,例如:
// This stay in the header
template<typename X>
void foo(x b);
// This goes into the source file - any drawbacks?
template<>
void foo(Bar b) {
...
}
是否有理由将其保留在标题而不是源文件中?我想将它们保留在源文件中,以减少编译时间(这些专用函数可能会变得非常大,并且它们在标头中拉取了很多依赖项(,但我想知道将它们放入源文件是否有任何权衡。
对于完全专用的模板,声明仍然应该在标头中(因此编译器知道它不使用主模板(:
template<> void foo(Bar b);
但是定义可以放在 cpp 文件中(或保留在标头中(:
template<>
void foo(Bar b) {
//...
}
相关文章:
- 是否有理由在标题中保留完全专用的模板?
- 如果我有很多具有相似前缀的字符串,是否有理由从该前缀创建一个子字符串?
- 如果代码中没有连接任何插槽,是否有理由发出Qt信号?
- 是否有理由对非负常量使用无符号类型?
- 是否有理由大多数/所有 try-catch 示例只对 throw 语句使用 void 子函数
- 是否仍然有理由在C++代码中使用"int"
- 是否有理由使用 malloc 初始化构造函数中的指针
- 是否有理由跳过for循环的初始化
- C++11:是否有理由为什么某些常规类型不应该专门使用“std::hash”
- 是否有理由不使用单元强制类型
- C++:是否有理由使用uint64_t而不是size_t
- 是否有理由使用字符串 => 索引到向量的映射,而不是字符串 => 对象?
- 有理由使用"::template"吗?
- 在这段代码中,是否有理由对字符串文字使用const_cast
- 是否有理由无法默认构造具有空捕获列表的 lambda?
- 是否有理由不从可执行文件中删除符号?
- 是否有理由显式声明模板类型
- 是否有理由在错误处理的代码中不广泛看到错误?
- 使用元组和boost.融合,有理由使用结构吗
- 是否有理由选择一个变量的多个unordered_map而不是结构体的多个unordered_map ?