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;

それ以外の場合、あなたtypeはタイプになり、作成することになっているタイプではなく、でも呼び出されるreplace_nth_type<...>として「返される」ためです。したがって、作成したものを取得する必要があります。typedeftype replace_nth_typetypename replace_nth_type<...>::typestd::tuple<...>

于 2013-03-17T12:36:05.240 に答える