c++ - Will instantiating templates in precompiled headers reduce compile times? -


example: include in precompiled header file:

#include <vector> 

as few instances of vector, such std::vector, std::vector etc used in project, reduce compile time if instantiate them in precomiled header this:

#include <vector> template class std::vector<float>; template class std::vector<int>; 

going further, make sense add dummy functions precompiled headers uses few functions:

namespace pch_detail { inline auto func() {   auto&& v = std::vector<float>{};   v.size();   v.begin();   v.front(); } } 

i'm unsure of of how translation units , templates work, seems me if instantiate them in precompiled headers, should mean not need instantiated every .cpp file.

update

tested on real-world code base visual studio 2017 , instantiations of commonly used template classes.

  1. with common templated class instantiated: 71731 ms
  2. without instantiation: 68544 ms

hence, @ least in case, took took more time.

it can make difference yes.

instantiation in translation units can exploit data in precompiled header, , compiler can read more c++ standard library headers.

but have maintain list of instantiations, compile-time optimisation might more trouble it's worth - idea end having opposite effect if have instantiations no longer needed.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -