 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Chris Guest
|
Posted: Thu Jan 27, 2005 2:03 am Post subject: Can standard algorithms add consts? |
|
|
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
|
Posted: Thu Jan 27, 2005 6:28 pm Post subject: Re: Can standard algorithms add consts? |
|
|
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
|
Posted: Sat Jan 29, 2005 3:26 am Post subject: Re: Can standard algorithms add consts? |
|
|
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 |
|
 |
|
|
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
|
|