如何使用显式模板实例化来减少编译时间? [英] How to use explicit template instantiation to reduce compilation time?

查看:96
本文介绍了如何使用显式模板实例化来减少编译时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

建议使用显式模板实例化以减少编译时间。我想知道如何去做。例如

It was suggesting to use explicit template instantiation to reduce compilation time. I am wondering how to do it. For example

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

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

But in every translation unit that a.h is included, it seems A<int> will be compiled. The compilation time is not reduced. How to use the explicit template instantiation to reduce compilation time?

推荐答案

如果您知道模板仅用于某些类型,
称为T1,T2,您可以将实现移到源文件中,就像普通类一样移至

If you know that your template will be used only for certain types, lets call them T1,T2, you can move implementation to source file, like normal classes.

//foo.hpp
template<typename T>
struct Foo {
    void f();
};

//foo.cpp
template<typename T>
void Foo<T>::f() {}

template class Foo<T1>;
template class Foo<T2>;

这篇关于如何使用显式模板实例化来减少编译时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆