简体   繁体   English

如何定义可变参数模板的参数?

[英]How can I define arguments of variadic template?

I have a variadic template and would to define the arguments using using alias declaration.我有一个可变参数模板,并且会using别名声明来定义参数。

Here is an example:这是一个例子:

template<class I, class... P>
struct Molecule {
  using Index = I;
};

My question is, how can I define the first argument of P... ?我的问题是,如何定义P...的第一个参数?

I have already tried this, but it generates an error:我已经尝试过了,但它会产生一个错误:

template<class I, class... P>
struct Molecule {
  using Index = I;
  P array[sizeof...(P)] = { P... };
  using Part1 = array[0];
};

Any suggestions please?请问有什么建议吗?

how i can define the first argument of P... ?我如何定义 P... 的第一个参数?

If the Molecule can be redefined to the following, you get it directly:如果Molecule可以重新定义为以下,则直接获取:

template<class I, class First, class... P> { 
    using Part1 = First;
    // .....
}

Otherwise, make a trait to find the first type:否则,创建一个特征来找到第一种类型:

template<typename First, typename... Rest> 
struct first { using type = First; };
template<typename... Args> using first_t = typename first<Args...>::type;

Now现在

template<class I, class... P> struct Molecule {  
    using Part1 = first_t<P...>; // first type
};

assuming that you would have at least one argument in variadic P... .假设您在可变参数P...中至少有一个参数。

See a demo查看演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM