samedi 23 décembre 2017

How to approach explicit template instantiation in the presence of unpredictable type aliases?

I am attempting to add some extern template into my project, to speed up build times and reduce footprint on disk during build.

I've done so by listing specialisations that I use frequently, e.g.

extern template class std::vector<std::string>;
extern template class std::vector<unsigned int>;
extern template class std::vector<uint32_t>;
extern template class std::vector<size_t>;

The problem is that unsigned int is size_t, maybe, and uint32_t is unsigned int, maybe. So, depending on the build target, compilation fails for the "non-extern" variety of the list that actually instantiates the specialisations:

template class std::vector<std::string>;
template class std::vector<unsigned int>;
template class std::vector<uint32_t>;
template class std::vector<size_t>;

The error is like this (line and column number inaccurate):

templates-impl.h:15:36: error: duplicate explicit instantiation of ‘class std::vector<long unsigned int>’ [-fpermissive]
 template class std::vector<size_t>;

My goal is to just be able to list the types I use, as I use them, with a minimum of fuss and with no need to hardcode variants of the list for different targets.

I guess in C++14 I could solve this with an if constexpr and some std::is_same.

How can I do it in C++11?

Aucun commentaire:

Enregistrer un commentaire