C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Can standard algorithms add consts?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Chris
Guest





PostPosted: Thu Jan 27, 2005 2:03 am    Post subject: Can standard algorithms add consts? Reply with quote



I was just wondering out of curosity if the algorithms in the C++
standard library are allowed to add consts? I noticed this recently when
I found various random compile-time messages appearing from a class
where I had not declared operator< const.

On one hand I can't see anything which tells me I have to declare
operator< (or operator==) const, on the other hand I imagine it would
make implementing the standard library somewhat harder.

Any comments?

Chris

---
[ 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.jamesd.demon.co.uk/csc/faq.html ]

Back to top
Daniel Krügler
Guest





PostPosted: Thu Jan 27, 2005 6:28 pm    Post subject: Re: Can standard algorithms add consts? Reply with quote



Hello Chris,

Chris schrieb:

Quote:
I was just wondering out of curosity if the algorithms in the C++
standard library are allowed to add consts? I noticed this recently
when I found various random compile-time messages appearing from a
class where I had not declared operator< const.

On one hand I can't see anything which tells me I have to declare
operator< (or operator==) const, on the other hand I imagine it would
make implementing the standard library somewhat harder.


You should definitely declare your overloaded relation operators either
as constant member
functions expecting constant arguments or as non-member functions
expecting both arguments
as immutable. The later is generally preferrable, but that is not the
point here.

Depending on the algorithm it is also definitly true, that an addition
might be performed (either on
the iterator or on its value type). This is either directly documented
(consider std::accumulate concerning +
or std::sort in the two-argument form, which delegates to std::less and
that itself usually delegates to
operator <) implicitly expectable (if the iterator must be incrementable
or even random-access-able)
by the requirements of the algorithm (i.e. std::sort).
Secondly it is actually a design error, if relational, logical, or
two-argument arithmetic operators
expect mutable arguments. This violates usual expectations on the
behaviour of such functions.
The only exception are **mutable** data members (which are declared as
such!), that do physically
change, but where the implementation guarantees that the class instance
itself remains **logically**
constant (Typically these members are needed for caching-like behaviour).
One example of this kind of caching can be found in most implementations
of std::istreambuf_iterator.
Iff your implementation of your operator< overload really needs
mutability, you should consider to

- rethink about its implementation and possibly refactor it (maybe
simply declaring your member
function as const is sufficient?).
- recognize possible mutable members of you class which are needed for
the mentioned caching
effects and declare them as such (If your compiler does **really**
not know the mutable
keyword, you should use const_cast inside your class!)
- if nothing of the former points applies, you should consider to rename
operator< to some named
member function, because obviously this function does **not** behave
like usual relational
operations!

Greetings from Bremen,

Daniel Krügler




---
[ 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.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Daniel Krügler
Guest





PostPosted: Sat Jan 29, 2005 3:26 am    Post subject: Re: Can standard algorithms add consts? Reply with quote




Daniel Krügler (ne Spangenberg) schrieb:

Quote:
Depending on the algorithm it is also definitly true, that an addition
might be performed (either on
the iterator or on its value type).

To prevent any unnecessary brooding concerning the sense of this
sentence ;-)

Of course I meant "less comparison" instead of "addition". I was mixing
up here two examples I
wanted to provide: One example concerning relational comparison (i.e.
<), and one concerning
a two-argument arithmetic operator (i.e. +). Traces of this attempt are
left in my example with
std::accumulate (which belongs to
I apologize for any inconvenience,

Daniel Krügler

---
[ 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.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.