Re: "auto" operators syntax
Andrei Polushin wrote:
Hi,
Most user-defined operators should have well-known semantics, so they
are implemented by pattern, written so many times by many people.
It might be quite annoying for experts, and error-prone for novices.
They have well defined semantics in certain situations, and in those
situations, I agree the lack of compiler support is really aggravating.
I propose to allow operators with "auto" modifier, so that compiler is
able to provide predefined implementation for them.
It looks like the following:
class A {
int first;
int second;
public:
A(int first, int second) auto;
A& operator+=(const A& a) auto;
static A operator+(const A& a, const A& b) auto;
A& operator++() auto;
A operator++(int) auto;
bool operator==(const A& a) const auto;
bool operator!=(const A& a) const auto;
bool operator<(const A& a) const auto;
bool operator>(const A& a) const auto;
bool operator>=(const A& a) const auto;
bool operator<=(const A& a) const auto;
};
The code to be generated is shown in /*...*/ comments:
class A {
int first;
int second;
public:
A(int first, int second) auto;
/*
: first(first), second(second) {}
*/
Well, I can sort of see this, but I write very few classes where the
only private data members are those initialised by the constructor.
There's probably some sort of argument for just having
A(...) auto; //Equivalent to a(int first_, int second_) :
first(first_), second(second_)
A& operator+=(const A& a) auto;
/*
first += a.first;
second += a.second;
return *this;
*/
This is not necessarily an intuitive implementation - OK for complex,
but for other things?
You should have also listed all the standard arithmetic operators.
<snip>
A& operator++() auto;
/*
++first;
++second;
return *this;
*/
I cannot believe this to be the case for any class with more than one
data member. I think A& operator++() { *this += 1; return *this } might
be more useful.
bool operator<(const A& a) const auto;
/*
return first < a.first
|| first == a.first && second < a.second
;
*/
Umm. This doesn't seem to be compatible with your ++.
Anyway, IIRC, there are some templates in boost that allow you to do
that sort of thing.
In addition, the same syntax should be allowed for main():
int main() auto;
/*
Do what I mean
*/
How on earth is the compiler meant to work out what you want to do?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]