Variadic template type deduction

I came across this wonderful article: http://pdimov.com/cpp2/simple_cxx11_metaprogramming.html

In the following code:

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* 

Why is C output as mp_list (instead of mp_list <int, float, void *> ) and T ... as int, float, void * ?

I think the trick is part of the template specialization: struct mp_rename_impl <C <T ...>, B> , but I can't understand why

+6
source share
1 answer

with

 mp_rename<mp_list<int, float, void*>, std::tuple>; 
  • in

     template<class A, template<class...> class B> using mp_rename = typename mp_rename_impl<A, B>::type; 

    A is mp_list<int, float, void*> , and B is std::tuple

  • in

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

    A is mp_list<int, float, void*> and B is std::tuple in the same way.

  • by specialization

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

    (I will rename to C to be clearer)
    C is mp_list , Ts... is int, float, void* , and B is std::tuple .

+4
source

Source: https://habr.com/ru/post/988443/


All Articles