如何定义variadic模板的参数?

发布于 2025-02-03 18:52:40 字数 411 浏览 3 评论 0原文

我有一个变异模板,将使用别名声明使用来定义参数。

这是一个示例:

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

我的问题是,如何定义p ...的第一个参数?

我已经尝试过,但是它会产生一个错误:

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

请提出任何建议吗?

I have a variadic template and would to define the arguments using using alias declaration.

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... ?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御守 2025-02-10 18:52:40

我如何定义p ...?

的第一个参数

如果可以将分子重新定义为以下内容,则可以直接获得:

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

否则,制定特征以找到第一种类型:

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

现在

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

假设您在variadic p中至少有一个参数。 ..

请参阅演示

how i can define the first argument of P... ?

If the Molecule can be redefined to the following, you get it directly:

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....

See a demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文