[英]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). 我想知道在编译时获取可变参数模板类的第N个参数的最简单和更常见的方法是什么(返回值必须作为编译器的静态const才能进行一些优化)。 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. 编辑:由于MyClass将包含200多个函数,我无法专门化它。 But I can specialize a struct or a function inside MyClass. 但我可以在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;
}
"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;
};
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 测试: http : //liveworkspace.org/code/4a1a9ed4edcf931373e7ab0bf098c847
and if you get sting by "cannot expand 'T...' into a fixed-length argument list" http://ideone.com/YF4UJ 并且如果你因为“无法扩展'T ...'到固定长度的参数列表中而感到刺痛” http://ideone.com/YF4UJ
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;
};
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: 顺便说一下,这是提取任何可变参数模板的第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.