Variadic模板类型演绎

Ste*_*ina 6 c++ templates metaprogramming c++11

我看到了这篇很棒的文章: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*
Run Code Online (Sandbox Code Playgroud)

为什么C被推导为mp_list(而不是mp_list <int,float,void*>)和T ...作为int,float,void*

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

Jar*_*d42 4

mp_rename<mp_list<int, float, void*>, std::tuple>;
Run Code Online (Sandbox Code Playgroud)
  • template<class A, template<class...> class B>
    using mp_rename = typename mp_rename_impl<A, B>::type;
    
    Run Code Online (Sandbox Code Playgroud)

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

  •  template<class A, template<class...> class B> struct mp_rename_impl;
    
    Run Code Online (Sandbox Code Playgroud)

    Aismp_list<int, float, void*>Bis 是std::tuple一样的。

  • 在专业化方面

    template<template<class...> class C, class... Ts, template<class...> class B>
    struct mp_rename_impl<C<Ts...>, B>
    
    Run Code Online (Sandbox Code Playgroud)

    (我将其重命名为C更清晰)
    Cis mp_listTs...isint, float, void*Bis std::tuple