Cannot inititialize std::function implicitly

后端 未结 2 1955
悲哀的现实
悲哀的现实 2021-01-14 18:42

I\'ve written this functor to perform an and operation (&&):

// unary functor; performs \'&&\'
template 
         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-14 19:15

    I suggest not abusing std::function and taking normal generic parameters instead. Treat std::function as a type-erased container for callable objects of a particular signature (see https://stackoverflow.com/a/11629125/46642 for more details).

    // unary functor; performs '&&'
    template 
    struct AND
    {
        X x;
        Y y;
    
        template 
        AND(XX&& xx, YY&& yy) 
                 : x(std::forward(xx)), y(std::forward(yy)) {}
    
        template 
        auto operator() ( const T &arg ) -> decltype(x(arg) && y(arg))
        { return x(arg) && y(arg); }
    };
    
    template 
    using Decay = typename std::decay::type;
    
    // helper
    template 
    AND, Decay> And(X&& xx, Y&& yy)
    {
        return AND, Decay>(std::forward(xx), std::forward(yy));
    }
    

提交回复
热议问题