I have a library with a class templated on a size:
foo.hpp:
template <size_t N>
struct Foo {
void bar();
};
foo.cpp:
template <size_t N>
void Foo<N>::bar() {
...
};
I want the compiled version to be instatiated for a specific list of sizes which would be defined in the cpp file. In pseudocode, something like this
foo.cpp:
template <size_t N>
void Foo<N>::bar() {
...
};
for (const auto & size: {1,2,7,9})
template struct Foo<size>;
Right now, I am manually doing this (instatiating for each type), but this is error prone since I have several files that should be defined for the same size.
I am aware that I could move everything to the header file, and use some enable_if's to choose which sizes are valid. However, I want to hide the implementation details. Hence the current approach.
EDIT:
FWIW, I was hoping to avoid macros. I thought that something like this would work if you want a range of variables (e.g. 1,2,3,4...):
template<template<size_t> class E, size_t I = 1, std::enable_if_t<I >= 5> *unused = nullptr>
bool instatiator() {
return true;
}
template<template<size_t> class E, size_t I = 1, std::enable_if_t<I < 5> *unused = nullptr>
bool instatiator() {
E<I> e;
return instatiator<E, I + 1>();
}
bool unused = instatiator<Encoder>();
but I can't seem to get that working.