0

我尝试使用THIS ANSWER来完成以下工作:(替换可变参数列表中的第 n 个元素并将其打包为元组)

template<typename... Ts>
using pack_as_tuple = std::tuple<Ts...>;


template< std::size_t N, typename T, typename... Ts>
struct replace_nth_type_in_list
{
    typedef replace_nth_type<N,T, pack_as_tuple<Ts...>> type;
};


int main()
{
    using U = std::tuple<std::string,unsigned,size_t,double>;
    using rep0 = replace_nth_type<0,char,U>::type;
    using rep1 = replace_nth_type<1,char,U>::type;
    using rep2 = replace_nth_type<2,char,U>::type;
    using rep3 = replace_nth_type<3,char,U>::type;
    static_assert(std::is_same<rep0, std::tuple<char,unsigned,size_t,double>>::value, "Error!");
    static_assert(std::is_same<rep1, std::tuple<std::string, char,size_t,double>>::value, "Error!");
    static_assert(std::is_same<rep2, std::tuple<std::string, unsigned,char,double>>::value, "Error!");
    static_assert(std::is_same<rep3, std::tuple<std::string, unsigned,size_t,char>>::value, "Error!");

    using repList0 = replace_nth_type_in_list<0,char,std::string,unsigned,size_t,double>::type;
    static_assert(std::is_same<repList0, std::tuple<char,unsigned,size_t,double>>::value, "Error!");
    return 0;
}

但是最后一个静态断言被触发了。你可以在这里看到现场例子 有人可以向我解释一下,为什么会发生这种情况以及如何解决这个问题?

4

1 回答 1

3

知道了!就是这一行:

typedef replace_nth_type<N,T, pack_as_tuple<Ts...>> type;

它应该是:

typedef typename replace_nth_type<N,T, pack_as_tuple<Ts...>>::type type;

因为否则你的typewill 是类型replace_nth_type<...>而不是它应该创建的类型,并且它被“返回”为typedef也称为type within 的 replace_nth_type类型。因此,您希望typename replace_nth_type<...>::type创建std::tuple<...>它。

于 2013-03-17T12:36:05.240 回答