Checking type of parameter pack using enable_if

前端 未结 5 2066
野的像风
野的像风 2021-02-19 21:38

Since there is a restriction on allowed non-type variadic templates, I am trying to write a function taking an arbitrary number of doubles using enable_if. In essen

5条回答
  •  粉色の甜心
    2021-02-19 22:15

    Here is a generic approach – a TMP for binary folding, using C++14. First, let's define the basic combining operations:

    #include 
    
    struct and_op
    {
        using type = bool;
        using identity = std::true_type;
        template  static constexpr bool value = A && B;
    };
    
    struct or_op
    {
        using type = bool;
        using identity = std::false_type;
        template  static constexpr bool value = A || B;
    };
    

    Now the actual fold mechanic:

    template 
    struct fold;
    
    template 
    struct fold : Op::identity {};
    
    template 
    struct fold
        : std::integral_constant {};
    
    template 
    struct fold
        : std::integral_constant::value>> {};
    

    Next, we need a way to create unary traits from binary traits by binding:

    template