joaocandre
joaocandre

Reputation: 1745

Variadic template declaration from single integer argument?

Is it possible to statically declare N same-type template arguments from single integer type template argument? Potentially something similar to this:

template < int N >
class MyTemplatedType {
    // base type, known
    typedef float base_t;
    // instance of some other templated class with *N* template arguments
    SomeOtherClass< base_t, base_t, base_t, base_t, ..., base_t > foo;
    /* ... */
}

I understand I can use a variadic template and use it directly to the member instance declaration, but I was wondering if there would be some kind of SFINAE implementation that would resolve a single integer template parameter, as the syntax would be much cleaner and intuitive.

Upvotes: 1

Views: 75

Answers (2)

cigien
cigien

Reputation: 60228

You can write a meta-function that accepts N, base_t, and SomeOtherClass, and recursively calls itself with a smaller N, each time tacking on base_t to the end of a growing parameter pack:

template <int N, typename T, template<typename...> typename C, typename ...Ts>
struct expand { 
    using type = typename expand<N-1, T, C, T, Ts...>::type;
};

For the base case, when N goes down to 0, the meta-function yields SomeOtherClass instantiated with the parameter pack of N base_t types:

template <typename T, template<typename...> typename C, typename ...Ts>
struct expand<0, T, C, Ts...> { 
    using type = C<Ts...>;
};

Also, convenience alias is nice to avoid having to say typename at the call site:

template <int N, typename T, template<typename...> typename C>
using expand_t = typename expand<N, T, C>::type;

Now at the call site you can write:

expand_t<N, base_t, SomeOtherClass> foo; 
// equivalent to 
// SomeOtherClass< base_t, base_t, ..., base_t > foo;
//               ^^^        N times          ^^^ 

Here's a demo.

Upvotes: 1

Sam Varshavchik
Sam Varshavchik

Reputation: 118350

One possible solution, using tuples and helper templates to assemble the desired class. A little additional sugar can make the resulting syntax "cleaner" Tested with gcc 10.2:

#include <tuple>
#include <type_traits>

// Create std::tuple<T ...>, with T repeated N times.

template<int N, typename T>
struct tuple_list : tuple_list<N-1, T> {

    typedef decltype(std::tuple_cat(std::declval<typename tuple_list<N-1, T>
                    ::tuple_t>(),
                    std::declval<std::tuple<T> &&>())
             ) tuple_t;
};

template<typename T>
struct tuple_list<0, T> {

    typedef std::tuple<> tuple_t;
};

template<typename ...> struct SomeOtherClass {};

// And now, replace `std::tuple` with SomeOtherClass 

template<typename tuple_t> struct make_some_other_class;

template<typename ...Args>
struct make_some_other_class<std::tuple<Args...>> {

    typedef SomeOtherClass<Args...> type_t;
};

template < int N >
class MyTemplatedType {

    typedef float base_t;

public:

    // The payload is not exactly "clean", but one more helper
    // template can make the syntax here a little bit nicer...

    typename make_some_other_class< typename tuple_list<N, base_t>
                    ::tuple_t >::type_t foo;
};

void foobar(MyTemplatedType<3> &bar)
{
    SomeOtherClass<float, float, float> &this_works=bar.foo;
}

Upvotes: 1

Related Questions