Let's say I have the following metafunction:
template <typename T>
struct make_pair {
using type = std::pair<
typename std::remove_reference<T>::type,
typename std::remove_reference<T>::type
>;
};
Would it improve compilation speed to do this (or something else) instead?
template <typename T>
struct make_pair {
using without_reference = typename std::remove_reference<T>::type;
using type = std::pair<without_reference, without_reference>;
};
I see two possibilities:
The compiler has to do some work every time it sees
typename std::remove_reference<T>::type
. Using an intermediate alias has some kind of "caching" behaviour, which allows the compiler to do some work only once.Compile-time performance is measured in terms of the number of template instantiations the compiler has to do. Because
std::remove_reference<T>::type
refers to the same type asstd::remove_reference<T>::type
, there is only one template instantiation required in both cases, so both implementations are equivalent WRT compile-time performance.
I think B is right, but I would like to be sure. If the answer turns out to be compiler specific, I would mostly be interested in knowing the answer for Clang and GCC.
Edit:
I benchmarked the compilation of a test program to have some data to work with. The test program does something like that:
template <typename ...> struct result;
template <typename T>
struct with_cache {
using without_reference = typename std::remove_reference<T>::type;
using type = result<without_reference, ..., without_reference>;
};
template <typename T>
struct without_cache {
using type = result<
typename std::remove_reference<T>::type,
...,
typename std::remove_reference<T>::type
>;
{ };
using Result = with[out]_cache<int>::type;
These are the average times for 10 compilations of the program, with 10 000 template parameters in result<>
.
-------------------------
| g++ 4.8 | clang++ 3.2 |
-----------------------------------------
| with cache | 0.1628s | 0.3036s |
-----------------------------------------
| without cache | 0.1573s | 0.3785s |
-----------------------------------------
The test program is generated by a script available here.
template
compiler that doesn't do memoization is going to be ridiculously slow. – Tankage