Search code examples
c++templatesvariadic-templates

C++ variadic template with tuple as template parameter


I would like to have something like this (warning: invalid code):

template<std::tuple<typename T, typename... Args>>
class nDimensionalPoint

This way, I could work with n-dimensional points one dimension at a time. Of course, I could avoid the std::tuple part by declaring template<typename T, typename... Args> and simply passing the coordinates in each dimension in a vector of its own, but that creates the following difficulty: One needs to somehow bind together the coordinates hidden in args.... Therefore, I would like a cleaner way of making it explicit that the underlying type is variadic tuple. How this could be done?


Solution

  • You might declare it as partial specialization as

    template<typename>
    class nDimensionalPoint;
    template<typename T, typename... Args>
    class nDimensionalPoint<std::tuple<T, Args...>> {};
    

    then use it like

    nDimensionalPoint<std::tuple<int, char>> dp ...;