dimanche 30 août 2015

C++11 alias template to alias template

Consider I have helper, which binds 1 template parameter to class template, lets say to int as first parameter to std::is_same:

template <class Base, template <typename...>class Exec>
struct t_bind_fst{
    template <class ...Type>
    using type = Exec<Base,Type...>;
};

Now, to use it, I have to :

template<class T>
using is_int = typename t_bind_fst<int, std::is_same >::type<T>;

And I can use is_int for type checking. Now look at the following code (see coments):

http://ift.tt/1NQFqM6

using namespace std;

template <class Base, template <typename...>class Exec>
struct t_bind_fst{
    template <class ...Type>
    using type = Exec<Base,Type...>;
};

/*
template <class Base, template <typename...>class Exec>
using t_bind_fst_t = t_bind_fst<Base, Exec>::template type;  // Why not work ?
*/


template <template<typename...>class Callback, class Cond, class Res, class ...Other>
struct switch_case_callback_t_impl{
    using type = std::conditional_t<
            Callback<Cond>::value,
            Res,
            typename switch_case_callback_t_impl<Callback, Other...>::type
    >;
};

template <template<typename...>class Callback, class Cond, class Res, class Default>
struct switch_case_callback_t_impl<Callback, Cond, Res, Default>{
    using type = std::conditional_t<
            Callback<Cond>::value,
            Res,
            Default
    >;
};


template < template<typename...>class Callback, class ...Conditions>
using switch_case_callback_t = typename switch_case_callback_t_impl<Callback, Conditions...>::type;

template <class Base, class ...Conditions>
using switch_is_same = switch_case_callback_t<
        t_bind_fst<Base, std::is_same>::template type,
        //t_bind_fst_t<Base, std::is_same>,            // want this shortcut
        Conditions...
>;


int main(){
    using T = int;
    using res = switch_is_same<T,
        int,  true_type,
        char, false_type,
        void
    >;

    cout << res::value;
    return 0;    
}

So the question is - why I can't have / or how I can have the following shortcut:

t_bind_fst_t<Base, std::is_same>

Aucun commentaire:

Enregistrer un commentaire