可变模板类型演绎
Variadic templates type deduction
我看到了一篇很棒的文章:http://pdimov.com/cpp2/simple_cxx11_metaprogramming.html
在以下代码中:
template<class A, template<class...> class B> struct mp_rename_impl;
template<template<class...> class C, class... T, template<class...> class B>
struct mp_rename_impl<C<T...>, B>
{
using type = B<T...>;
};
template<class A, template<class...> class B>
using mp_rename = typename mp_rename_impl<A, B>::type;
//...
mp_rename<mp_list<int, float, void*>, std::tuple>; // -> std::tuple<int, float, void*>
// T... will be deduced as int, float, void*
为什么C被演绎为mp_list(而不是mp_list<int,>)和T…作为int, float, void* ?
我认为诀窍在于模板特化部分: struct mp_rename_impl
with
mp_rename<mp_list<int, float, void*>, std::tuple>;
template<class A, template<class...> class B> using mp_rename = typename mp_rename_impl<A, B>::type;
A
为mp_list<int, float, void*>
,B
为std::tuple
template<class A, template<class...> class B> struct mp_rename_impl;
A
=mp_list<int, float, void*>
,B
=std::tuple
。专门化
template<template<class...> class C, class... Ts, template<class...> class B> struct mp_rename_impl<C<Ts...>, B>
(我重命名为
C
更清楚)C
为mp_list
,Ts...
为int, float, void*
,B
为std::tuple
。
相关文章:
- 类型演绎 C++ 标准和自动
- λ类型演绎失败
- decltype(auto) 类型演绎:返回 x 与返回 (x)
- 使用decltype的动态多态类型演绎
- 普遍类型演绎scott-meyers
- c++ 11使用std::函数进行类型演绎
- 使用通用引用时进行类型演绎
- c++如何在无法进行类型演绎时调用模板化构造函数
- c#泛型中的类型演绎类似于c++模板
- 类型演绎不适用于std::function
- 为什么decltype返回类型在递归模板中失败,而返回类型演绎却工作得很好?
- 表达式模板中的按引用捕获可以与类型演绎共存
- 类型演绎模板函数c++
- 模板,类型演绎不足
- 括号初始化列表和函数模板类型演绎顺序
- 类型演绎的重载赋值操作符
- 静态方法中派生类的类型演绎
- 在类型演绎之后,函数模板中的替换顺序是否有任何保证
- 返回类型演绎是否可能
- c++ 14中赋值时的返回类型演绎