Search code examples
c++templatesc++11usingvariadic-templates

Variadic templates and type definition with 'using'


I wrote a small using statement to have easy access to the types of a variadic template parameter pack.

template<size_t index, typename args...>
using get = std::tuple_element<index, std::tuple<args...>>::type;

But compiling this with clang (3.5.0) or gcc (4.9.0) fails. Here is the error output of clang:

error: expected ',' or '>' in template-parameter-list template<size_t index, typename args...>
                                                                                          ^

Is the using statement not combinable with variadic templates? Or am I doing something wrong?


Solution

  • Two things are wrong in your code.

    First, since the definition of std::tuple_element<...> depends on what ... is, you need to tell the compiler that ::type refers to a type name, hence you need typename in front of it.

    Second, variadic template parameters have their ellipsis in between typename (or class) and the name of the pack, i.e. typename ...args instead of typename args....

    So the fixed code is then:

    template<size_t index, typename ...args>
    using get = typename std::tuple_element<index, std::tuple<args...>>::type;