从可变模板列表中替换第n个元素 [英] replacing the n-th element from a variadic template list
问题描述
我尝试使用此答案来获得以下工作:
(替换第n个元素
I tried to use THIS ANSWER to get the following working: (replacing the n-th element from a variadic list and packing it as a tuple)
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;
}
但是最后一个静态断言被触发。您可以查看实际示例这里
有人可以向我解释,为什么会发生这种情况,如何解决这个问题?
But the last static assert is triggered. You can see the live example HERE Can somebody explain to me, why this happens and how to solve this?
推荐答案
这是这行:
typedef replace_nth_type<N,T, pack_as_tuple<Ts...>> type;
应为:
typedef typename replace_nth_type<N,T, pack_as_tuple<Ts...>>::type type;
因为否则您的类型
replace_nth_type< ...>
,而不是它应该创建的类型,以及返回为 typedef $ c $在
replace_nth_type
中也称为类型
因此,您希望 typename replace_nth_type< ...> :: type
获取 std :: tuple< ...>
它创建。
because otherwise your type
will be of type replace_nth_type<...>
and not the type that it is supposed to create and which is "returned" as a typedef
that is also called type
within replace_nth_type
. Hence you want the typename replace_nth_type<...>::type
to get the std::tuple<...>
it created.
这篇关于从可变模板列表中替换第n个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!