I wish C++ generated more default operators (like I don't get why I need to define both += and +, < and > and >= and <= and so on). I wish I could just go operator+=(const SameClass&) = default; if I write the related classes.
I'm not sure I feel confident with that. That's a potentially very large number of automatically generated functions which you would have to '= delete' or specify directly if they are not doing what you want them to. Very easy to miss one of them too. I'd much rather have something like boost::operators as part of the standard instead of something automatic.
I agree they shouldn't be auto-generated without explicit permission. I was suggesting the use of
= default; to tell the compiler you *want* the compiler to generate the operator, but by default, they are automatically
= delete;(Bah, I keep on using 'default' when I mean compiler-generated upon request)
Perhaps "default" wouldn't be a good keyword. Maybe = auto; keyword could be repurposed for this.
class MyClass
{
public:
MyClass operator+(const MyClass &other); //Manually specified.
MyClass &operator=(const MyClass &other);
//Explicitely tell the compiler to generate an optimized function based off of combing the + and = operators' logic,
//but with optimized instructions.
//Compiler generates: { return this->operator=(this->operator+(other)); }, But optimized to remove the unnecessary copy.
MyClass &operator+=(const MyClass &other) = auto;
//Can work with other types also, ofcourse, and also in reverse direction.
MyClass &operator+=(int value); /Manually specified.
//Compiler generates: { return MyClass(*this).operator+(value); }, But optimized.
MyClass operator+(int value) = auto;
};
And ditto for comparison operators, logic operators, binary operators, etc... but only for each function that you explicitly say
= auto; Does that make more sense?