Vincent
Vincent

Reputation: 60481

Easiest way to get the N-th argument of a variadic templated class?

I wonder what is the easiest and more common way to get the N-th parameter of a variadic templated class at compile-time (The returned value has to be as a static const for the compiler in order to do some optimizations). Here is the form of my templated class :

template<unsigned int... T> MyClass
{
    // Compile-time function to get the N-th value of the variadic template ?
};

Thank you very much.

EDIT : As MyClass will contain more than 200 functions, I can't specialize it. But I can specialize a struct or a function inside MyClass.

EDIT : Final solution derived from the validated answer :

#include <iostream>

template<unsigned int... TN> class MyClass
{
    // Helper
    template<unsigned int index, unsigned int... remPack> struct getVal;
    template<unsigned int index, unsigned int In, unsigned int... remPack> struct getVal<index, In,remPack...>
    {
        static const unsigned int val = getVal<index-1, remPack...>::val;
    };
    template<unsigned int In, unsigned int...remPack> struct getVal<1,In,remPack...>
    {
        static const unsigned int val = In;
    };

    // Compile-time validation test
    public:
        template<unsigned int T> inline void f() {std::cout<<"Hello, my value is "<<T<<std::endl;}
        inline void ftest() {f<getVal<4,TN...>::val>();} // <- If this compile, all is OK at compile-time
};
int main()
{
    MyClass<10, 11, 12, 13, 14> x;
    x.ftest();
    return 0;
}

Upvotes: 16

Views: 6137

Answers (4)

markhor
markhor

Reputation: 2282

Yet another simple method:

#include <array>

template<int... Args>
class Foo {
 public:
  static constexpr int Element(int index) {
    return std::array<int, sizeof...(Args)>{ Args... }[index];
  }

 int First = Element(0);
 int Second = Element(1);
};


int main() {
  return Foo<0, 1>::Element(0);  
}

// or just
int Nth = std::array<int, sizeof...(Args)>{ Args... }[N];

BTW, here is a general method of extracting N-th argument of any variadic template:

#include <tuple>

template<typename... Args>
class Foo {
 public:
  template <int N>
  using Nth = typename std::remove_reference<decltype(std::get<N>(std::declval<std::tuple<Args...>>()))>::type;
};


int main() {
  Foo<int, float>::Nth<1> val = 3.14159f;
  return 0; 
}

Upvotes: 3

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507313

Here is what you can also do

template<int N, typename T, T ... Ts>
struct GetN {
  constexpr T values[sizeof...(Ts)] = { Ts... };
  static const T value = values[N];
};

template<int N, typename T, T ... Ts>
constexpt T GetN<N, T, Ts...>::values[sizeof...(Ts)];

Then you can simply do

template<int N, unsigned int... T> struct MyClass {
  static const unsigned int value = GetN<N, unsigned int, T...>::value;
};

Upvotes: 5

Mr.Anubis
Mr.Anubis

Reputation: 5342

Here is another way to do it:

template<unsigned int index, unsigned int In, unsigned int... remPack> struct getVal
{
    static const unsigned int val = getVal<index-1, remPack...>::val;
};
template<unsigned int In, unsigned int...remPack> struct getVal<0,In,remPack...>
{
    static const unsigned int val = In;
};

template<unsigned int... T> struct MyClass
{
    //go to any arg by : getVal<Some_Unsigned_Index, T...>::val;
};

Test: http://liveworkspace.org/code/4a1a9ed4edcf931373e7ab0bf098c847

and if you get sting by "cannot expand 'T...' into a fixed-length argument list" http://ideone.com/YF4UJ

Upvotes: 6

Ben Voigt
Ben Voigt

Reputation: 283803

"Design by induction" should come out something like this:

template<unsigned int N, unsigned int Head, unsigned int... Tail>
struct GetNthTemplateArgument : GetNthTemplateArgument<N-1,Tail...>
{
};


template<unsigned int Head, unsigned int... Tail>
struct GetNthTemplateArgument<0,Head,Tail...>
{
    static const unsigned int value = Head;
};

template<unsigned int... T> 
class MyClass
{
     static const unsigned int fifth = GetNthTemplateArgument<4,T...>::value;
};

Upvotes: 10

Related Questions