Why do several of the standard operators not have standard functors?

后端 未结 6 1072
庸人自扰
庸人自扰 2021-02-05 05:15

We have:

  • std::plus (+)
  • std::minus (-)
  • std::multiplies (*)
6条回答
  •  说谎
    说谎 (楼主)
    2021-02-05 06:21

    I think the most likely answer to the question is that the included operators are the ones that were thought to be most useful. If no one thinks to add something to the Standard Library, it won't get added.

    I think the assertion that the operator functors are useless in C++0x because lambda expressions are superior is silly: sure, lambda expressions are wonderful and far more flexible, but sometimes using a named functor can lead to terser, cleaner, easier to understand code; in addition, named functors can be polymorphic while lambdas cannot.

    The Standard Library operator functors are, of course, not polymorphic (they are class templates, so the operand types are part of the type of the functor). It's not particularly difficult to write your own operator functors, though, and macros make the task quite straightforward:

    namespace ops
    {
        namespace detail
        {
            template 
            T&& declval();
    
            template 
            struct remove_reference      { typedef T type; }
    
            template 
            struct remove_reference  { typedef T type; }
    
            template 
            struct remove_reference { typedef T type; }
    
            template 
            T&& forward(typename remove_reference::type&& a)
            {
                return static_cast(a);
            }
    
            template 
            T&& forward(typename remove_reference::type& a)
            {
                return static_cast(a);
            }
    
            template 
            struct subscript_impl
            {
                subscript_impl(T&& arg) : arg_(arg) {}
    
                template 
                auto operator()(U&& u) const ->
                    decltype(detail::declval()[detail::declval()])
                {
                    return u[arg_];
                }
            private:
                mutable T arg_;
            };
        }
    
        #define OPS_DEFINE_BINARY_OP(name, op)                              \
            struct name                                                     \
            {                                                               \
                template                            \
                auto operator()(T&& t, U&& u) const ->                      \
                    decltype(detail::declval() op detail::declval())  \
                {                                                           \
                    return detail::forward(t) op detail::forward(u);  \
                }                                                           \
            }
    
        OPS_DEFINE_BINARY_OP(plus,               +  );
        OPS_DEFINE_BINARY_OP(minus,              -  );
        OPS_DEFINE_BINARY_OP(multiplies,         *  );
        OPS_DEFINE_BINARY_OP(divides,            /  );
        OPS_DEFINE_BINARY_OP(modulus,            %  );
    
        OPS_DEFINE_BINARY_OP(logical_or,         || );
        OPS_DEFINE_BINARY_OP(logical_and,        && );
    
        OPS_DEFINE_BINARY_OP(equal_to,           == );
        OPS_DEFINE_BINARY_OP(not_equal_to,       != );
        OPS_DEFINE_BINARY_OP(less,               <  );
        OPS_DEFINE_BINARY_OP(greater,            >  );
        OPS_DEFINE_BINARY_OP(less_equal,         <= );
        OPS_DEFINE_BINARY_OP(greater_equal,      >= );
    
        OPS_DEFINE_BINARY_OP(bitwise_and,        &  );
        OPS_DEFINE_BINARY_OP(bitwise_or,         |  );
        OPS_DEFINE_BINARY_OP(bitwise_xor,        ^  );
        OPS_DEFINE_BINARY_OP(left_shift,         << );
        OPS_DEFINE_BINARY_OP(right_shift,        >> );
    
        OPS_DEFINE_BINARY_OP(assign,             =  );
        OPS_DEFINE_BINARY_OP(plus_assign,        += );
        OPS_DEFINE_BINARY_OP(minus_assign,       -= );
        OPS_DEFINE_BINARY_OP(multiplies_assign,  *= );
        OPS_DEFINE_BINARY_OP(divides_assign,     /= );
        OPS_DEFINE_BINARY_OP(modulus_assign,     %= );
        OPS_DEFINE_BINARY_OP(bitwise_and_assign, &= );
        OPS_DEFINE_BINARY_OP(bitwise_or_assign,  |= );
        OPS_DEFINE_BINARY_OP(bitwise_xor_assign, ^= );
        OPS_DEFINE_BINARY_OP(left_shift_assign,  <<=);
        OPS_DEFINE_BINARY_OP(right_shift_assign, >>=);
    
        #define OPS_DEFINE_COMMA() ,
        OPS_DEFINE_BINARY_OP(comma, OPS_DEFINE_COMMA());
        #undef OPS_DEFINE_COMMA
    
        #undef OPS_DEFINE_BINARY_OP
    
        #define OPS_DEFINE_UNARY_OP(name, pre_op, post_op)                  \
        struct name                                                         \
        {                                                                   \
            template                                            \
            auto operator()(T&& t) const ->                                 \
                decltype(pre_op detail::declval() post_op)               \
            {                                                               \
                return pre_op detail::forward(t) post_op;                \
            }                                                               \
        }
    
        OPS_DEFINE_UNARY_OP(dereference,      * ,   );
        OPS_DEFINE_UNARY_OP(address_of,       & ,   );
        OPS_DEFINE_UNARY_OP(unary_plus,       + ,   );
        OPS_DEFINE_UNARY_OP(logical_not,      ! ,   );
        OPS_DEFINE_UNARY_OP(negate,           - ,   );
        OPS_DEFINE_UNARY_OP(bitwise_not,      ~ ,   );
        OPS_DEFINE_UNARY_OP(prefix_increment, ++,   );
        OPS_DEFINE_UNARY_OP(postfix_increment,  , ++);
        OPS_DEFINE_UNARY_OP(prefix_decrement, --,   );
        OPS_DEFINE_UNARY_OP(postfix_decrement,  , --);
        OPS_DEFINE_UNARY_OP(call,               , ());
        OPS_DEFINE_UNARY_OP(throw_expr,   throw ,   );
        OPS_DEFINE_UNARY_OP(sizeof_expr, sizeof ,   );
    
        #undef OPS_DEFINE_UNARY_OP
    
        template 
        detail::subscript_impl subscript(T&& arg)
        {
            return detail::subscript_impl(detail::forward(arg));
        }
    
        #define OPS_DEFINE_CAST_OP(name, op)                                \
            template                                       \
            struct name                                                     \
            {                                                               \
                template                                   \
                Target operator()(Source&& source) const                    \
                {                                                           \
                    return op(source);                              \
                }                                                           \
            }
    
        OPS_DEFINE_CAST_OP(const_cast_to,       const_cast      );
        OPS_DEFINE_CAST_OP(dynamic_cast_to,     dynamic_cast    );
        OPS_DEFINE_CAST_OP(reinterpret_cast_to, reinterpret_cast);
        OPS_DEFINE_CAST_OP(static_cast_to,      static_cast     );
    
        #undef OPS_DEFINE_CAST_OP
    
        template 
        struct get_data_member
        {
            template 
            auto operator()(T&& arg) const ->
                decltype(detail::declval().*PointerToMember)
            {
                return arg.*PointerToMember;
            }
        };
    
        template 
        struct get_data_member_via_pointer
        {
            template 
            auto operator()(T&& arg) const ->
                decltype(detail::declval()->*PointerToMember)
            {
                return arg->*PointerToMember;
            }
        };
    }
    

    I have omitted new and delete (and their various forms) because it is too difficult to write exception-safe code with them :-).

    The call implementation is limited to nullary operator() overloads; with variadic templates you might be able to extend this to support a wider range of overloads, but really you'd be better off using lambda expressions or a library (like std::bind) to handle more advanced call scenarios. The same goes for the .* and ->* implementations.

    The remaining overloadable operators are provided, even the silly ones like sizeof and throw.

    [The above code is standalone; no Standard Library headers are required. I admit I am a bit of a noob still with respect to rvalue references, so if I've done something wrong with them I hope someone will let me know.]

提交回复
热议问题