2015-06-03 31 views
6

natknąłem się na ten wielki artykuł: http://pdimov.com/cpp2/simple_cxx11_metaprogramming.htmlo zmiennej liczbie argumentów szablony typu odliczenie

W poniższym kodzie:

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* 

Dlaczego C jest wyprowadzany jako mp_list (zamiast mp_list < int, float , void *>) i T ... jako int, float, void *?

myślę Sztuką jest częścią szablonu specjalność: struct mp_rename_impl < C < T ...> B>, Ale ja usiłuję zrozumieć dlaczego

+0

Które "A"? 'class A' lub' template klasa A'? – Jarod42

+0

Zmieniłem nazwę na C, tak jak uczyniłeś to, aby było jaśniejsze. –

Odpowiedz

4

z

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

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

    A jest mp_list<int, float, void*> i B jest std::tuple

  • w

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

    A jest mp_list<int, float, void*> i B jest std::tuple w taki sam sposób.

  • w specjalizacji

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

    (zmienić nazwę do C się jaśniejsze)
    C jest mp_list, Ts... jest int, float, void* i B jest std::tuple.

+0

Z jakiegoś powodu zabrało mi to trochę czasu, ale zastanawiałem się, jak możemy podzielić 'mp_list < Ts... >' na 'mp_list' i' Ts ... '. Ale 'struct mp_rename_impl < C< Ts... >, B>' specjalizuj 'szablon klasa B> struct mp_rename_impl;', który lepiej pasuje, jeśli 'A' ->' mp_list '. Specjalizacja 'struct mp_rename_impl < C< Ts... >, B>' pozwala nam oddzielić 'C' i' Ts ... ' –

Powiązane problemy