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 

What operators to overload when using STL containers?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Anand Hariharan
Guest





PostPosted: Sun Jul 25, 2004 11:01 am    Post subject: What operators to overload when using STL containers? Reply with quote



Hello all. This is my first post in this group.

I have two questions, motivation for which arises from Microsoft ATL's
CAdapt class (vide [url]http://tinyurl.com/3ze94)[/url].

1/ "... many container classes (such as the STL container classes)
expect to be able to obtain the addresses of their contained objects
using the address-of operator." Is the expectation of the ampersand
operator being usable in its vanilla sense a QoI issue, or is it
"encouraged" by the standard?

2/ Besides "=", "<" and "==", what operators should I be wary of
overloading before I use a (STL) container to hold my own class'
objects?
I am aware than an exhaustive answer to this question depends upon
what containers & algorithms I intend to use. As such, I don't have a
specific requirement - just want to be aware of gotchas.

thank you & best wishes,
Anand Hariharan

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Maciej Sobczak
Guest





PostPosted: Sun Jul 25, 2004 5:56 pm    Post subject: Re: What operators to overload when using STL containers? Reply with quote



Hi,

Anand Hariharan wrote:
Quote:
Hello all. This is my first post in this group.

I have two questions, motivation for which arises from Microsoft ATL's
CAdapt class (vide [url]http://tinyurl.com/3ze94)[/url].

1/ "... many container classes (such as the STL container classes)
expect to be able to obtain the addresses of their contained objects
using the address-of operator." Is the expectation of the ampersand
operator being usable in its vanilla sense a QoI issue, or is it
"encouraged" by the standard?

Address-of operator is included in the CopyConstructible requirements
and all types intended for use with STL should comply.
This means that you should ensure that the &t expression does "the right
thing", which means that it should result in the addres of t.
This requirement is stated in the 20.1.3, in table 30.

Quote:
2/ Besides "=", "<" and "==", what operators should I be wary of
overloading before I use a (STL) container to hold my own class'
objects?

The contained objects need to be assignable. This means that operator=
is of utmost importance. They should be also copyable, so do not forget
about copy constructors.
For comparison operators, it all depends on what you want to do.
A common sense helps a lot: if you want to perform the find algorithm on
the sequence, it is reasonable to have working operator==.
If you want to lexicographicaly compare sequences or use associative
containers, either provide operator< or be sure to use your own functors
when the algorithm allows it.
Of course, good practice (see below) requires that you implement all
operators from the same "family", which means that if you have
operator==, write also operator!=. If you have operator<, write also
operator>, operator<= and operator>=.

Quote:
I am aware than an exhaustive answer to this question depends upon
what containers & algorithms I intend to use. As such, I don't have a
specific requirement - just want to be aware of gotchas.

One of the gotchas is that not all library implementations are strict
when it comes to the requirements. Formally, it should be enough for
your types to implement operator< (if you intend to use algorithms that
may depend upon it), and the library should use *only* this form. In
reality, some implementations depend also on the others, like operator>.
The same applies to operator== (which is required) and operator!= (which
is good to have).

--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) 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.