Currying for templates in C++ metaprogramming

前端 未结 2 679
深忆病人
深忆病人 2021-02-20 05:58

This is more of a conceptual question. I\'m trying to find the easiest way of converting a two-arg template (the arguments being types) into a one-arg template. I.e., binding on

相关标签:
2条回答
  • 2021-02-20 06:17

    Yeah, I had this issue to. It took a few iterations to figure out a decent way to do this. Basically, to do this, we need to specify a reasonable representation of what we want and need. I borrowed some aspects from std::bind() in that I want to specify the template that I wish to bind and the parameters that I want to bind to it. Then, within that type, there should be a template that will allow you to pass a set of types.

    So our interface will look like this:

    template <template <typename...> class OP, typename...Ts>
    struct tbind;
    

    Now our implementation will have those parameters plus a container of types that will be applied at the end:

    template <template <typename...> class OP, typename PARAMS, typename...Ts>
    struct tbind_impl;
    

    Our base case will give us a template type, which I'll call ttype, that'll return a template of the contained types:

    template <template <typename...> class OP, typename...Ss>
    struct tbind_impl<OP, std::tuple<Ss...>>
    {
        template<typename...Us>
        using ttype = OP<Ss...>;
    };
    

    Then we have the case of moving the next type into the container and having ttype refer to the ttype in the slightly simpler base case:

    template <template <typename...> class OP, typename T, typename...Ts, typename...Ss>
    struct tbind_impl<OP, std::tuple<Ss...>, T, Ts...>
    {
        template<typename...Us>
        using ttype = typename tbind_impl<
              OP
            , std::tuple<Ss..., T>
            , Ts...
        >::template ttype<Us...>;
    };
    

    And finally, we need a remap of the templates that will be passed to ttype:

    template <template <typename...> class OP, size_t I, typename...Ts, typename...Ss>
    struct tbind_impl<OP, std::tuple<Ss...>, std::integral_constant<size_t, I>, Ts...>
    {
        template<typename...Us>
        using ttype = typename tbind_impl<
              OP
            , typename std::tuple<
                  Ss...
                , typename std::tuple_element<
                      I
                    , typename std::tuple<Us...>
                  >::type
              >
            , Ts...
        >::template ttype<Us...>;
    

    Now, since programmers are lazy, and don't want to type std::integral_constant<size_t, N> for each parameter to remap, we specify some aliases:

    using t0 = std::integral_constant<size_t, 0>;
    using t1 = std::integral_constant<size_t, 1>;
    using t2 = std::integral_constant<size_t, 2>;
    ...
    

    Oh, almost forgot the implementation of our interface:

    template <template <typename...> class OP, typename...Ts>
    struct tbind : detail::tbind_impl<OP, std::tuple<>, Ts...>
    {};
    

    Note that tbind_impl was placed in a detail namespace.

    And voila, tbind!

    Unfortunately, there is a defect prior to c++17. If you pass tbind<parms>::ttype to a template that expects a template with a particular number of parameters, you will get an error as the number of parameters don't match (specific number doesn't match any number). This complicates things slightly requiring an additional level of indirection. :(

    template <template <typename...> class OP, size_t N>
    struct any_to_specific;
    
    template <template <typename...> class OP>
    struct any_to_specific<OP, 1> 
    {
        template <typename T0>
        using ttype = OP<T0>;
    };
    
    template <template <typename...> class OP>
    struct any_to_specific<OP, 2>
    {
        template <typename T0, typename T1>
        using ttype = OP<T0, T1>;
    };
    ...
    

    Using that to wrap tbind will force the compiler to recognize the template having the specified number of parameters.

    Example usage:

    static_assert(!tbind<std::is_same, float, t0>::ttype<int>::value, "failed");
    
    static_assert( tbind<std::is_same, int  , t0>::ttype<int>::value, "failed");
    
    static_assert(!any_to_specific<
          tbind<std::is_same, float, t0>::ttype
        , 1
    >::ttype<int>::value, "failed");
    
    static_assert( any_to_specific<
          tbind<std::is_same, int  , t0>::ttype
        , 1
     >::ttype<int>::value, "failed");
    

    All of which succeed.

    0 讨论(0)
  • 2021-02-20 06:20

    I think the typical way of doing this is keep everything in the world of types. Don't take template templates - they're messy. Let's write a metafunction named ApplyAnInt that will take a "metafunction class" and apply int to it:

    template <typename Func>
    struct ApplyAnInt {
        using type = typename Func::template apply<int>;
    };
    

    Where a simple metafunction class might be just checking if the given type is an int:

    struct IsInt {
        template <typename T>
        using apply = std::is_same<T, int>;
    };
    
    static_assert(ApplyAnInt<IsInt>::type::value, "");
    

    Now the goal is to support:

    static_assert(ApplyAnInt<std::is_same<_, int>>::type::value, "");
    

    We can do that. We're going to call types that contain _ "lambda expressions", and write a metafunction called lambda which will either forward a metafunction class that isn't a lambda expression, or produce a new metafunction if it is:

    template <typename T, typename = void>
    struct lambda {
        using type = T;
    };
    
    template <typename T>
    struct lambda<T, std::enable_if_t<is_lambda_expr<T>::value>>
    {
        struct type {
            template <typename U>
            using apply = typename apply_lambda<T, U>::type;
        };
    };
    
    template <typename T>
    using lambda_t = typename lambda<T>::type;
    

    So we update our original metafunction:

    template <typename Func>
    struct ApplyAnInt
    {
        using type = typename lambda_t<Func>::template apply<int>;
    };
    

    Now that leaves two things: we need is_lambda_expr and apply_lambda. Those actually aren't so bad at all. For the former, we'll see if it's an instantiation of a class template in which one of the types is _:

    template <typename T>
    struct is_lambda_expr : std::false_type { };
    
    template <template <typename...> class C, typename... Ts>
    struct is_lambda_expr<C<Ts...>> : contains_type<_, Ts...> { };
    

    And for apply_lambda, we just will substitute the _ with the given type:

    template <typename T, typename U>
    struct apply_lambda;
    
    template <template <typename...> class C, typename... Ts, typename U>
    struct apply_lambda<C<Ts...>, U> {
        using type = typename C<std::conditional_t<std::is_same<Ts, _>::value, U, Ts>...>::type;
    };
    

    And that's all you need actually. I'll leave extending this out to support arg_<N> as an exercise to the reader.

    0 讨论(0)
提交回复
热议问题