We have:
std::plus(+)std::minus(-)std::multiplies(*)std::divides(/)std::modulus(%)std::negate(-)std::logical_or(||)std::logical_not(!)std::logical_and(&&)std::equal_to(==)std::not_equal_to(!=)std::less(<)std::greater(>)std::less_equal(<=)std::greater_equal(>=)
We don’t have functors for:
&(address-of)*(dereference)[],- bitwise operators
~,&,|,^,<<,>> ++(prefix/postfix) /--(prefix/postfix)sizeofstatic_cast/dynamic_cast/reinterpret_cast/const_cast- c style casts
new/new[]/delete/delete[]- all of the member function pointer operators
- all of the compound assignment operators.
Is there a reason we don’t have those, or is it just an oversight?
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:
I have omitted
newanddelete(and their various forms) because it is too difficult to write exception-safe code with them :-).The
callimplementation is limited to nullaryoperator()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 (likestd::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
sizeofandthrow.[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.]