Fixed Length Variadic Parameter Packs in C++11

c++, c++11, gcc, templates, variadic-templates

Solution

Well, you can use std::enable_if:

template <typename... Args,
    typename = typename std::enable_if<
        sizeof...(Args) == n
    >::type>
explicit Vector(Args&&... values) : _data{ std::forward<Args>(values)... } {}

It will shadow constructors that may accept size of Args other than n.

Problem

I'm attempting to implement a generalized n-dimensional vector class using C++11. Ideally, I'd like to provide the type "T" and number of dimensions "n" of the vector and have the constructor accept the appropriate number of arguments. Unfortunately, I've been unable to find a way to allow a template-specified fixed length of a parameter pack. What I'm looking for is something like ``` template<typename T, size_t n> class Vector { public: Vector(T... values /* values is exactly n parameters long */); ... }; ``` Is it possible to do this?

Original source