相关疑难解决方法(0)

模板元编程:为什么扁平类型失败

我想将树型扁平化为扁平型.例:

typedef std::tuple<int,std::tuple<int,long>,int> tup; 
Flat<tup>::type=>std::tuple<int,int,long,int>
Run Code Online (Sandbox Code Playgroud)

我用:

template<typename T>
struct Flat
{
    using type=T;
};
template <template <typename ...> class C,typename...ARGS>
struct Flat<C<ARGS...> >
{
    using type=C<ARGS...>;
};
template <template <typename ...> class C,typename ...ARGS0,typename...ARGS1,typename ...ARGS2>
struct Flat<C<ARGS0...,C<ARGS1...>,ARGS2...> >
:Flat<C<ARGS0...,ARGS1...,ARGS2...> >
{

};

void test(){
typedef std::tuple<int,std::tuple<int,long>,int> tup;
static_assert(std::is_same<typename Flat<tup>::type,std::tuple<int,int,long,int> >::value,"");
}
Run Code Online (Sandbox Code Playgroud)

但我std::tuple<int,std::tuple<int,long>,int>还是静止...我使用gcc 4.8.1

c++ templates c++11

5
推荐指数
1
解决办法
467
查看次数

标签 统计

c++ ×1

c++11 ×1

templates ×1