I want to create an instance of the variation template class Store<TArgs...> that has std::vector for each type in the TArgs... package TArgs...
template<typename... TArgs> class Store { // obviously not valid code // assuming each type of TArgs... has a `unsigned int` id that can be // retrieved with getId<T>() std::array<sizeof...(TArgs), std::vector<TArgs...>> bags; template<typename T> void add(T mValue) { bags[getId<T>()].push_back(mValue); } template<typename T> std::vector<T>& get() { return bags[getId<T>()]; } };
Let's say I have Store<int, float, double> . I obviously know, at compile time, that it will be able to store int , float and double values.
I could use specialized templates:
template<> class Store<int, float, double> { std::vector<int> vi; std::vector<float> vf; std::vector<double> vd; template<typename T> void add(T); template<> void add<int>(int mValue) { vi.push_back(mValue); } template<> void add<float>(float mValue) { vf.push_back(mValue); } template<> void add<double>(double mValue) { vd.push_back(mValue); }
... but this will require manual spelling of every possible combination of types and will not work with user-defined types.
I am sure that the compiler knows everything that is required to create a class like Store<int, float, double> using variable templates - is there any way to express this intention?
c ++ c ++ 11 templates variadic-templates
Vittorio romeo
source share