gpt4 book ai didi

c++ - 反转元组参数

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:24:19 24 4
gpt4 key购买 nike

我想编写一个模板类 InvTuple,它将 type 定义为逆序类参数的元组。所以它应该像

InvTuple<T1, T2, T3, ...>::type   --->   tuple<..., T3, T2, T1>

我是这样定义的

template<class...T>
struct InvTuple;

template<class T1, class...T>
struct InvTuple < T1, T... >
{
template<class... U>
using doInvert = typename InvTuple<T...>::doInvert < U..., T1 > ;
// <--- unrecognizable template declaration/definition,
// syntax error : '<'

using type = doInvert<>;
};

template<>
struct InvTuple <>
{
template<class... U>
using doInvert = tuple<U...>;

using type = doInvert < > ;
};

但是由于代码中显示的错误,这不会编译。请帮助我了解问题所在。

最佳答案

您需要模板关键字:

using doInvert = typename InvTuple<T...>::template doInvert < U..., T1 > ;

并且您还需要在同一行中切换 U...T1 以使其正常工作:

#include <iostream>
#include <tuple>
#include <typeinfo>
using namespace std; // Don't try this at home

template<class...T>
struct InvTuple;

template<class T1, class...T>
struct InvTuple < T1, T... >
{
template<class... U>
using doInvert = typename InvTuple<T...>::template doInvert < T1, U... >;

using type = doInvert<>;
};

template<>
struct InvTuple <>
{
template<class... U>
using doInvert = tuple<U...>;

using type = doInvert < > ;
};

int main()
{
InvTuple<int,char,bool> obj;
InvTuple<int,char,bool>::type obj2;
cout << typeid(obj).name() << endl; // InvTuple<int, char, bool>
cout << typeid(obj2).name() << endl; // std::tuple<bool, char, int>
}

Example

关于c++ - 反转元组参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26277718/

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