簡體   English   中英

獲得可變模板類的第N個參數的最簡單方法是什么?

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

我想知道在編譯時獲取可變參數模板類的第N個參數的最簡單和更常見的方法是什么(返回值必須作為編譯器的靜態const才能進行一些優化)。 這是我的模板類的形式:

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

非常感謝你。

編輯:由於MyClass將包含200多個函數,我無法專門化它。 但我可以在MyClass中專門化一個結構或函數。

編輯:從經過驗證的答案得出的最終解決方案:

#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;
}

“感應設計”應該是這樣的:

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;
};

這是另一種方法:

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;
};

測試: http//liveworkspace.org/code/4a1a9ed4edcf931373e7ab0bf098c847

並且如果你因為“無法擴展'T ...'到固定長度的參數列表中而感到刺痛” http://ideone.com/YF4UJ

這也是你也可以做的

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)];

然后你就可以做到

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

又一個簡單的方法:

#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];

順便說一下,這是提取任何可變參數模板的第N個參數的一般方法:

#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; 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM