Throw multiple templates into a template template - template binding?

Given the following class:

template <class T, template <typename> class B>
class A { B<T> b; };

Now I can write this code:

A<float, MyVector> a1;
A<int, MySet> a2;

What is the most elegant way to place multiparameter classes for which all but one parameter are specified in B? How is a card with int keys? The only thing I can think of is this:

template <class U> using C = MyMap<int, U>;
A<float, C<int>> a3;

Is there such a template equivalent to std :: bind where we can provide only part of the parameters and leave one of them open? I am quite sure that the language does not provide for this, but people must have decided this before.

A<float, MyMap<int, _>> a3;
+4
source share
1 answer

, std::bind, . , , :

template <typename T, template <typename...> class B>
struct bind_t1 {
    template <typename... Ts>
    using type = B<T,Ts...>;   
};

bind_t1 :

A<float, bind_t1<int, std::map>::type> a3;

, , Variadic:

template <class T, template <typename...> class B>
class A { B<T> b; };

, :

template <template <typename...> class B, typename... Ts>
struct bind_nt1 {
    template <typename... Us>
    using type = B<Ts...,Us...>;   
};

//Usage
A<std::less<int>, bind_nt1<std::map, int, float>::type> a3;

, std::bind, . , , , . .

template <std::size_t N> 
struct placeholder{};

template <template <typename...> class B, typename... Ts>
struct bind_t {
private:
    template <typename T, typename UTuple>
    struct resolve_placeholder {
        using type = T;
    };

    template <std::size_t N, typename UTuple>
    struct resolve_placeholder<placeholder<N>, UTuple> {
        using type = typename std::tuple_element<N-1, UTuple>::type;
    };

public:
    template <typename... Us>
    using type = B<typename resolve_placeholder<Ts, std::tuple<Us...>>::type...>;
};


//Usage 
A<int, bind_t<std::map, float, placeholder<1>, std::less<float>>::type> a3;

, :

//std::map<int,float>
bind_t<std::map, placeholder<2>, placeholder<1>>::type<float, int> b;
+4

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


All Articles