I have a template struct with a non-type parameter, each instantiation of it takes a long time to compile. I know realistically I will only need to instantiate it for a few dozen or so different size_t parameters. A minimal example would be something along the lines of:
///foo.h///
template <size_t A>
struct Foo{
void bar();
};
extern template struct Foo<1>;
extern template struct Foo<2>;
extern template struct Foo<3>;
// and so on...
extern template struct Foo<25>;
///foo.cpp///
#include "A.h"
template <size_t A>
void Foo<A>::bar(){
// Do something that depends on A at compile time and
// has lots of expression templates so it takes a while
// to compile
}
template struct Foo<1>;
template struct Foo<2>;
template struct Foo<3>;
// and so on...
template struct Foo<25>;
///main.cpp///
#include "A.h"
int main(){
//Use any Foo<n> for n in 1...25 here without needing
//template instantiation in this translation unit
return 0;
}
Is there any way using template metaprogramming to avoid having to write out all 25 explicit instantiations in foo.h and foo.cpp? Or will I have to use a macro for this?