gpt4 book ai didi

c++ - 模板参数包访问第 N 个类型和第 N 个元素

转载 作者:IT老高 更新时间:2023-10-28 12:34:14 25 4
gpt4 key购买 nike

以下论文是我找到的第一个关于模板参数包的建议。

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1603.pdf

在第 16 页,它谈到了引入两个新的运算符 [] 和 <> 来访问参数包元素和参数包类型。

The suggested syntax for such an operator involves two new operators: .[] to access values and .<> to access types. For instance:

template<int N, typename Tuple> struct tuple_element;
template<int N, ... Elements>
struct tuple_element<tuple<Elements...> >
{
typedef Elements.<N> type;
};

template<int N, ... Elements>
Elements.<N>& get(tuple<Elements...>& t)
{ return t.[N]; }

template<int N, ... Elements>
const Elements.<N>& get(const tuple<Elements...>& t)
{ return t.[N]; }

那么这些运算符在哪里?如果没有,他们的替代品是什么?

最佳答案

其他人已经回答可以通过 std::tuple 完成。如果您想访问第 N 种类型的参数包,您可能会发现以下元函数很方便:

template<int N, typename... Ts> using NthTypeOf =
typename std::tuple_element<N, std::tuple<Ts...>>::type;

用法:

using ThirdType = NthTypeOf<2, Ts...>;

关于c++ - 模板参数包访问第 N 个类型和第 N 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20162903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com