 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
gast128 Guest
|
Posted: Sat Nov 27, 2004 4:00 am Post subject: standard operators |
|
|
Dear all,
probably posted already a hundred times, but after working with c++
more than 6 years I still wondering why there is no standard compiler
generated operator== and operator<. If you use this classes for
sorting, you always have to fill in the operator, and 99% it will be
that every member is checked.
There is however an automatic generated operator=. Is the rationale
for this decision that the assignment operator= is used in more
contexts than the other operators?
Wkr,
me
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Mickey Moore Guest
|
Posted: Sat Nov 27, 2004 9:29 pm Post subject: Re: standard operators |
|
|
| Quote: | probably posted already a hundred times, but after working with c++
more than 6 years I still wondering why there is no standard compiler
generated operator== and operator<. If you use this classes for
sorting, you always have to fill in the operator, and 99% it will be
that every member is checked.
|
I suppose operator== could be defined automatically as memberwise
comparison. But the existing automatically generated member
functions cause enough trouble as it is; why add more? You usually
have to implement them yourself (or suppress them) anyway and you
must at least check that not doing so is correct.
There is no obvious general implementation of operator<. And I
disagree strongly that every member would be checked in operator<
"99% [of the time]".
For many (most?) classes, operator< is ill-defined and should be
illegal (e.g., std:complex, spatial vectors, etc.). If it was
automatically generated you'd end up having to suppress it for some
classes just like you must sometimes suppress operator=.
I suppose a standard predicate function for use in sorting and sorted
containers could be useful, but don't call it less or operator<
unless it truly means "less than".
| Quote: | There is however an automatic generated operator=. Is the rationale
for this decision that the assignment operator= is used in more
contexts than the other operators?
|
The point here is not to break C code/constructs when moving it to
C++. In C, assigning a struct to another does memberwise (or perhaps
even bitwise) copy; that behavior had to be preserved in C++, hence
the default operator=. I think all of the automatically generated
member functions have this rationale.
C structs can't be compared, so there were no backward compatibility
issues there.
-- Mickey Moore
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Andrew Koenig Guest
|
Posted: Sun Nov 28, 2004 2:10 am Post subject: Re: standard operators |
|
|
"gast128" <gast128 (AT) hotmail (DOT) com> wrote
| Quote: | There is however an automatic generated operator=. Is the rationale
for this decision that the assignment operator= is used in more
contexts than the other operators?
|
The rationale is that C allows assignment of structures, so it is useful for
C++ to behave in a compatible way.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Stephen Howe Guest
|
Posted: Sun Nov 28, 2004 11:37 am Post subject: Re: standard operators |
|
|
| Quote: | probably posted already a hundred times, but after working with c++
more than 6 years I still wondering why there is no standard compiler
generated operator== and operator<. If you use this classes for
sorting, you always have to fill in the operator, and 99% it will be
that every member is checked.
|
Not always.
| Quote: | There is however an automatic generated operator=. Is the rationale
for this decision that the assignment operator= is used in more
contexts than the other operators?
|
No. Backward compatibility with C.
With C's structs can be created, destroyed, copied and assigned but not
compared for equality.
And that is why C++ compilers have to be able to default generate ctors,
dtors, cctors, and assignment operators.
Stephen Howe
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Raymond Martineau Guest
|
Posted: Sun Nov 28, 2004 11:52 pm Post subject: Re: standard operators |
|
|
<adb37573.0411261158.54984dd8 (AT) posting (DOT) google.com>
On 26 Nov 2004 23:00:40 -0500, [email]gast128 (AT) hotmail (DOT) com[/email] (gast128) wrote:
| Quote: | Dear all,
probably posted already a hundred times, but after working with c++
more than 6 years I still wondering why there is no standard compiler
generated operator== and operator<.
|
The compiler does not necessairly know how you want to assert equality in
the structure or class. For example, your data structure may contain
something like this:
class MyClass
{
protected:
char *array; /* Pointer to a string. */
}
In this case, the compiler does not know how array should be evaluated for
a comparision, because this data type isn't always a null-terminated
string. (It could instead be a buffer being filled for File I/O.)
| Quote: | There is however an automatic generated operator=. Is the rationale
for this decision that the assignment operator= is used in more
contexts than the other operators?
|
Operator = is a copy operator, used to directly transfer the firleds of a
struct in C. It works basically the same way in C++ where it directly
copies the members, in the same way it copies the fields of a struct.
The difference with C++ is that you can overload the operator so that you
can copy the contents of the buffer instead of just copying the pointer.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
gast128 Guest
|
Posted: Sun Nov 28, 2004 11:53 pm Post subject: Re: standard operators |
|
|
"Stephen Howe" <sjhoweATdialDOTpipexDOTcom (AT) eu (DOT) uu.net> wrote
| Quote: | probably posted already a hundred times, but after working with c++
more than 6 years I still wondering why there is no standard compiler
generated operator== and operator<. If you use this classes for
sorting, you always have to fill in the operator, and 99% it will be
that every member is checked.
Not always.
Therefore I said 99%. In our projects, the operator== and operator |
are most needed in the context of putting them in a std::map or
std::set (or sorted std::vector), so that these containers can sort on
it, and that you can find them back.
If you develop large projects in c++, you have too think of a LOT of
things. If you add a member variable to a class, you have to see if it
has user defined operators and adjust them if needed. While this is
not difficult or spectacular, practice has learned us that this tend
to be forgotten, and this leads then to hard to track bugs.
It would be far more easier (also for the operator=) if the compiler
had built in support to do the operand for all its members. Then this
will never lead to a bug, like:
const X& X::operator=(const X& r)
{
if (&r != this)
{
//call default
__compiler_for_all_members_operator=(r);
//override a member for deep copy
m_p = new ...
}
return *this;
}
C++ gives you a lot of flexibility also for large projects, but
sometimes it would be nice if the compiler could help here and there.
Besides this discussion about operators, there other things, but I
will come back to that in another thread.
Wkr,
me
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Talbut Guest
|
Posted: Mon Nov 29, 2004 10:56 am Post subject: Re: standard operators |
|
|
"gast128" <gast128 (AT) hotmail (DOT) com> wrote
| Quote: | "Stephen Howe" <sjhoweATdialDOTpipexDOTcom (AT) eu (DOT) uu.net> wrote in
message news:<41a9233b$0$8155$cc9e4d1f (AT) news (DOT) dial.pipex.com>...
probably posted already a hundred times, but after working with
c++
more than 6 years I still wondering why there is no standard
compiler
generated operator== and operator<. If you use this classes for
sorting, you always have to fill in the operator, and 99% it
will be
that every member is checked.
Not always.
Therefore I said 99%. In our projects, the operator== and operator
are most needed in the context of putting them in a std::map or
std::set (or sorted std::vector), so that these containers can sort
on
it, and that you can find them back.
If you develop large projects in c++, you have too think of a LOT of
things.
|
I disagree entirely.
Most, more than 50%, never mind 1%, of the classes I produce are small
classes with specific uses in specific contexts.
For these classes I actually spend more time disabling the copy
constructor and operator= than I do defining them.
Having additional things to disable would just make my job more
difficult.
Yes, when you are dealing with classes that represent "normal" data it
makes sense to use operator== and operator< but there are a great many
classes that don't represent "normal" data.
Some examples: classes that exist to ensure resource freeing; classes
that exist to do work on other classes; functional classes.
The mere concept of operator< does not make sense for any of these.
As you say, in a large project in c++ you do have to think of a lot of
things.
I think that the more explicit those things are the more usable the
language is - the ability to define operator< provides power, having
it happen automatically provides confusion.
I actually mandate that all non-trivial classes (and preferably the
trivial ones too) should declare their own copy constructor and
operator=, this way the users of the class can see what is happening.
Realistically it's probably a moot point anyway, adding this feature
now would break far too much.
What might be more use could be some kind of 'thing' that caused an
operation to take place on each member of a class.
Having an automatic way to call operator< on each base and then on
each member would ease the burden on you.
Having an automatic way to call swap() on each base and then on each
member would ease the burden on me.
It would need to be like some kind of in built function template (that
took either a functional object or a predicate as an argument).
I don't think it could be done with the facilities currently
available.
Any ideas anyone?
J.T.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Stephen Howe Guest
|
Posted: Mon Nov 29, 2004 9:49 pm Post subject: Re: standard operators |
|
|
| Quote: | Not always.
Therefore I said 99%. In our projects, the operator== and operator
are most needed in the context of putting them in a std::map or
std::set (or sorted std::vector), so that these containers can sort on
it, and that you can find them back.
|
Lets do something concrete:
class Something
{
public:
Something() : a_(0), b_(0), c_(0) {}
Something(int a, int b, int c) : a_(a), b_(b), c_(c) {}
private:
int a_;
int b_;
int c_;
};
Now, what would you recommend for operator < that the compiler should
automatically generate if not specified? Bear in mind you would like to
insert Something object's in a std::set, so the compiler-generated operator
< has to obey strict weak ordering.
Stephen Howe
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Mickey Moore Guest
|
Posted: Tue Nov 30, 2004 10:54 am Post subject: Re: standard operators |
|
|
| Quote: | Lets do something concrete:
class Something
{
public:
Something() : a_(0), b_(0), c_(0) {}
Something(int a, int b, int c) : a_(a), b_(b), c_(c) {}
private:
int a_;
int b_;
int c_;
};
Now, what would you recommend for operator < that the compiler should
automatically generate if not specified? Bear in mind you would like to
insert Something object's in a std::set, so the compiler-generated operator
has to obey strict weak ordering.
|
Well this function would implement such a strict weak ordering:
bool std_order_pred(const Something& Thing1, const Something& Thing2)
{
bool result = (Thing1.a_ < Thing2.a_);
result = result || ((Thing1.a_ == Thing2.a_)
&& (Thing1.b_ < Thing2.b_));
result = result || ((Thing1.b_ == Thing2.b_)
&& (Thing1.c_ < Thing2.c_));
// Where's the ||= operator when you need it? ;)
return result;
}
This is easily generalizable and is the conceptual equivalent of a
lexigraphical compare; you sort on the first non-equal member.
You can generalize for types without an operator== by changing
(Thing1.a_ == Thing2.a_) to !(Thing2.a_ < Thing1.a_).
But note I refused to use the name operator< for this function! This
function provides a useful ordering in some contexts, but not a
"natural" ordering. operator< must mean "less than" or you are just
confusing people. (I didn't name this predicate less() for the same
reason.)
I do often provide this function for my classes where operator< is
not appropriate -- which is almost all of them! -- so that I can put
them in associative containers easily later.
std::pair does provide an operator< (similar to above) for pragmatic
reasons; std:map would be confusing to implement and use without it.
But the committee refused (correctly, in my opinion) to provide a
specialization of std::less for std::complex, because there is no
"canonical" (fully mathematically consistent) ordering for complex
numbers. (Note that I wouldn't be averse to the library including
some utility ordering predicates for such classes though -- just
don't name them operator< or std::less.)
-- Mickey Moore
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
gast128 Guest
|
Posted: Tue Nov 30, 2004 11:03 am Post subject: Re: standard operators |
|
|
| Quote: | For these classes I actually spend more time disabling the copy
constructor and operator= than I do defining them.
Boost has a nice boost::noncopyable base class, from which you can |
derive. It serves also as a documentary hint. But actually this is
also a 'feature' which I don't like about c++ in large projects: most
of the classes are not copyable by default, or at least I haven't
think about it (e.g. due to deep copy), so I don't want my compiler to
generate the copy ctor/operator=. So now almost all classes which I
produce are derived from this boost::noncopyable.
| Quote: | Realistically it's probably a moot point anyway, adding this feature
now would break far too much.
I am aware of that, but c++ grows and grows and keeps its old |
constructs, because of zillions legacy code. A cleanup would be nice,
perhaps like with c, where you can compile with the old k&r style.
| Quote: | What might be more use could be some kind of 'thing' that caused an
operation to take place on each member of a class.
Having an automatic way to call operator< on each base and then on
each member would ease the burden on you.
That's what I proposed in a previous mail. It solves many of my |
problems. Typing is reduced to a minimum, and it has many advantages
while not creating a maintenance object. And you can always override
the members you want (e.g. for deep copying purposes, specialized
operator==) or not using it at all.
Are there other c++ guru's who can react on this?
Wkr,
me
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dave Harris Guest
|
Posted: Tue Nov 30, 2004 11:01 pm Post subject: Re: standard operators |
|
|
[email]stephenPOINThoweATtns-globalPOINTcom (AT) eu (DOT) uu.net[/email] (Stephen Howe) wrote
(abridged):
| Quote: | class Something
{
public:
Something() : a_(0), b_(0), c_(0) {}
Something(int a, int b, int c) : a_(a), b_(b), c_(c) {}
private:
int a_;
int b_;
int c_;
};
Now, what would you recommend for operator < that the compiler should
automatically generate if not specified?
|
It should surely default to lexical ordering in member declaration order.
Eg:
bool operator<( const Something &lhs, const Something &rhs ) {
if (lhs.a_ != rhs.a_)
return lhs.a_ < rhs.a_;
if (lhs.b_ != rhs.b_)
return lhs.b_ < rhs.b_;
return lhs.c_ < rhs.c_;
}
-- Dave Harris, Nottingham, UK
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Tue Nov 30, 2004 11:13 pm Post subject: Re: standard operators |
|
|
In article <adb37573.0411291557.74fac7fa (AT) posting (DOT) google.com>, gast128
<gast128 (AT) hotmail (DOT) com> writes
| Quote: | What might be more use could be some kind of 'thing' that caused an
operation to take place on each member of a class.
Having an automatic way to call operator< on each base and then on
each member would ease the burden on you.
That's what I proposed in a previous mail. It solves many of my
problems. Typing is reduced to a minimum, and it has many advantages
while not creating a maintenance object. And you can always override
the members you want (e.g. for deep copying purposes, specialized
operator==) or not using it at all.
|
Part of the explicit class proposal (N1717) from Lois Goldthwaite and
myself provides a mechanism for telling a compiler to generate a default
definition of a function. In the current proposal it is limited to the
special member functions but if there was sufficient support it could be
extended to other cases. However I would strongly resist introducing
other implicit definitions, indeed part of the motive for the explicit
class proposal is exactly that these implicit definitions are often
unhelpful and have to be suppressed by using hacks.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Tue Nov 30, 2004 11:34 pm Post subject: Re: standard operators |
|
|
[email]gast128 (AT) hotmail (DOT) com[/email] (gast128) wrote in message
news:<adb37573.0411261158.54984dd8 (AT) posting (DOT) google.com>...
| Quote: | probably posted already a hundred times, but after working with c++
more than 6 years I still wondering why there is no standard compiler
generated operator== and operator<. If you use this classes for
sorting, you always have to fill in the operator, and 99% it will be
that every member is checked.
|
I don't know from where you got your statistics, but in 15 years of C++,
I don't think I've ever had a class where such a default generated <
would do the right thing. And such a default == would be generally
wrong too, as soon as there are pointers.
| Quote: | There is however an automatic generated operator=. Is the rationale
for this decision that the assignment operator= is used in more
contexts than the other operators?
|
The rationale is that it was a necessary evil, for reasons of C
compatiblity. It's the same rationale which gives us a default
constructor, a copy constructor and a destructor. Without those four,
you can't have a struct that works like a struct in C would.
Ideally, I think there wouldn't be any compiler generated defaults. But
that would break C compatibility, and if we were going that route, there
are a lot of other things, like the declaration syntax, which need
fixing as well.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Mickey Moore Guest
|
Posted: Tue Nov 30, 2004 11:53 pm Post subject: Re: standard operators |
|
|
| Quote: | Boost has a nice boost::noncopyable base class, from which you can
derive. It serves also as a documentary hint. But actually this is
also a 'feature' which I don't like about c++ in large projects: most
of the classes are not copyable by default, or at least I haven't
think about it (e.g. due to deep copy), so I don't want my compiler to
generate the copy ctor/operator=. So now almost all classes which I
produce are derived from this boost::noncopyable.
|
So you want to remove the automatic copy ctor/operator= because they
cause trouble, but you want to add an automatic operator== and
operator
-- Mickey Moore
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
Posted: Wed Dec 01, 2004 10:38 am Post subject: Re: standard operators |
|
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
| Quote: | The rationale is that it was a necessary evil, for reasons of C
compatiblity. It's the same rationale which gives us a default
constructor, a copy constructor and a destructor. Without those four,
you can't have a struct that works like a struct in C would.
Ideally, I think there wouldn't be any compiler generated defaults. But
that would break C compatibility, and if we were going that route, there
are a lot of other things, like the declaration syntax, which need
fixing as well.
|
It could have been made so that classes didn't have any compiler
generated defaults while structs and unions kept C compatibility;
I wonder why it has not been made that way. A mere oversight?
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|