gpt4 book ai didi

c++ - 如何使用显式模板实例化来减少编译时间?

转载 作者:可可西里 更新时间:2023-11-01 14:54:49 25 4
gpt4 key购买 nike

建议使用显式模板实例化来减少编译时间。我想知道该怎么做。例如

// a.h
template<typename T> class A {...};
template class A<int>; // explicit template instantiation to reduce compilation time

但在每个包含 a.h 的翻译单元中,似乎都是 A<int>将被编译。编译时间没有减少。如何使用显式模板实例化来减少编译时间?

最佳答案

在头部声明实例化:

extern template class A<int>;

并在一个源文件中定义它:

template class A<int>;

现在它只会被实例化一次,而不是在每个翻译单元中,这可能会加快速度。

关于c++ - 如何使用显式模板实例化来减少编译时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26247711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com