可变参数模板类型推导

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) and T... as int, float, void* ?

我认为诀窍在于模板专业化部分: struct mp_rename_impl< C < T... >, B>, 但我很难理解为什么

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;
    

    Amp_list<int, float, void*>Bstd::tuple

  •  template<class A, template<class...> class B> struct mp_rename_impl;
    

    Amp_list<int, float, void*>Bstd::tuple,同理

  • 专业中

    template<template<class...> class C, class... Ts, template<class...> class B>
    struct mp_rename_impl<C<Ts...>, B>
    

    (为了更清楚,我重命名为C
    Cmp_listTs...int, float, void*Bstd::tuple