473,721 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interface of std::map's erase member functions

Dinkumware's online STL reference http://tinyurl.com/3es52 declares
std::map's overloaded erase member functions to have the interface:

map::erase

iterator erase(iterator where);
iterator erase(iterator first, iterator last);
size_type erase(const Key& keyval);

Ie, the first two functions above have the same interface as
std::vector returning an iterator

My copy of gcc's routines declares that the interface is:

void erase(iterator where);
void erase(iterator first, iterator last);
size_type erase(const Key& keyval);

As does the SGI online docs at http://tinyurl.com/u2uy

Am I correct in saying that Dinkumware have got it wrong?
Regards,
Angus

Jul 22 '05 #1
5 2757
On Wed, 4 Feb 2004 17:07:54 +0000 (UTC), Angus Leeming
<an***********@ btopenworld.com > wrote:
Dinkumware's online STL reference http://tinyurl.com/3es52 declares
std::map's overloaded erase member functions to have the interface:

map::erase

iterator erase(iterator where);
iterator erase(iterator first, iterator last);
size_type erase(const Key& keyval);

Ie, the first two functions above have the same interface as
std::vector returning an iterator

My copy of gcc's routines declares that the interface is:

void erase(iterator where);
void erase(iterator first, iterator last);
size_type erase(const Key& keyval);

As does the SGI online docs at http://tinyurl.com/u2uy

Am I correct in saying that Dinkumware have got it wrong?


Yes - their signature isn't conforming. However, it is difficult to
detect this problem with conforming code, since you can't rely on any
particular signature for non-virtual standard library member
functions. Use the post increment technique to make your code
conforming.
Not:
i = m.erase(i); //non-conforming, relies on Dinkumware extension
but the roughly equivalent:
m.erase(i++); //conforming.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #2
tom_usenet wrote:
Am I correct in saying that Dinkumware have got it wrong?
Yes - their signature isn't conforming. However, it is difficult to
detect this problem with conforming code, since you can't rely on
any particular signature for non-virtual standard library member
functions. Use the post increment technique to make your code
conforming.


Thanks Tom.

Actually, I'm trying to write a single lambda function wrapper
for all of the stl containers' erase signatures.

I guess something tasting like the boost lambda library will
make it into some future standard, so the code below isn't totally
off topic. Just mostly ;-)

Angus
struct erase {

/*
This mouthful can differentiate between the generic erase functions
(Container == deque, list, vector) and that
specific to the two map-types, std::map and std::multimap.

iterator Container::eras e(iterator where);
iterator Container::eras e(iterator first, iterator last);

size_type Map::erase(cons t Key& keyval);
void Map::erase(iter ator where);
void Map::erase(iter ator first, iterator last);

*/
template <typename T,
typename Arg1,
typename Arg2 = nil_t>
struct result
{
typedef
boost::mpl::app ly_if<
boost::mpl::or_ <
is_std_map<T>
, is_std_multimap <T> >
, boost::mpl::ide ntity<void>
, iterator_of<T> stage_1;

typedef typename
boost::mpl::app ly_if<
boost::mpl::and _<
boost::is_same< Arg1, typename key_type_of<Arg 1>::type>
, boost::is_same< Arg2, nil_t> >
, size_type_of<T>
, stage_1::type

type;
};

template <typename T, typename Arg1>
typename result<T, Arg1>::type
operator()(T & c, Arg1 const & arg1) const
{
return c.erase(arg1);
}

template <typename T>
typename result<T, typename T::iterator, typename T::iterator>::t ype
operator()(T & c,
typename T::iterator const & from,
typename T::iterator const & to) const
{
return c.erase(from, to);
}
};
Jul 22 '05 #3
"tom_usenet " <to********@hot mail.com> wrote in message:

Yes - their signature isn't conforming. However, it is difficult to
detect this problem with conforming code, since you can't rely on any particular signature for non-virtual standard library member
functions.


Doesn't this do the trick?

void f(std::map<int, int>& m)
{
std::map<int, int>::iterator it = m.begin();
if (it != m.end()) return m.erase(it);
}

This should compile only if map::erase returns void.

Jonathan
Jul 22 '05 #4
On Wed, 4 Feb 2004 11:01:18 -0700, "Jonathan Turkanis"
<te******@kanga roologic.com> wrote:
"tom_usenet " <to********@hot mail.com> wrote in message:

Yes - their signature isn't conforming. However, it is difficult to
detect this problem with conforming code, since you can't rely on

any
particular signature for non-virtual standard library member
functions.


Doesn't this do the trick?

void f(std::map<int, int>& m)
{
std::map<int, int>::iterator it = m.begin();
if (it != m.end()) return m.erase(it);
}

This should compile only if map::erase returns void.


True, I hadn't thought of that, but who ever returns a void value from
a void function? I suppose I should rephrase - it is unlikely that
conforming code will ever be affected by the invalid return type, but
it is possible.

The problem is that Dinkumware doesn't want to break code written to
one of their old, pre-standard libraries that also returned the
iterator, but wasn't non-conforming since there was no standard to
conform to.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #5
In article <cl************ *************** *****@4ax.com>,
tom_usenet <to********@hot mail.com> wrote:
On Wed, 4 Feb 2004 11:01:18 -0700, "Jonathan Turkanis"
<te******@kanga roologic.com> wrote:
"tom_usenet " <to********@hot mail.com> wrote in message:

Yes - their signature isn't conforming. However, it is difficult to
detect this problem with conforming code, since you can't rely on

any
particular signature for non-virtual standard library member
functions.


Doesn't this do the trick?

void f(std::map<int, int>& m)
{
std::map<int, int>::iterator it = m.begin();
if (it != m.end()) return m.erase(it);
}

This should compile only if map::erase returns void.


True, I hadn't thought of that, but who ever returns a void value from
a void function? I suppose I should rephrase - it is unlikely that
conforming code will ever be affected by the invalid return type, but
it is possible.

The problem is that Dinkumware doesn't want to break code written to
one of their old, pre-standard libraries that also returned the
iterator, but wasn't non-conforming since there was no standard to
conform to.


Fwiw, this is LWG issue 130:

http://anubis.dkuug.dk/jtc1/sc22/wg2...ctive.html#130

Years ago it was voted as "not a defect" by the LWG. But the issue has
recently been reopened (at my request) in a new light: Ok, so it was an
intentional design decision for C++98. But for C++0X would it be better
to change the return type of these functions to iterator?

Several of the "NAD" defect reports are now being reexamined within the
context of C++0X.

-Howard
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
17315
by: Duane Hebert | last post by:
2 questions: Given a map defined as std::map<int,string> stringmap; //How do you access the data_type (string) via an iterator? std::string spoo("doh");
26
5393
by: Pieter Thysebaert | last post by:
Hello, I've got a question conerning erasing key-value pairs from a std::map while iterating over it. According to the STL docs, erasing an element in a map invalidates all iterators pointing to that element so
2
3399
by: Serengeti | last post by:
Hello, in my class I have a map that translates strings to pointers to some member functions. The code goes like this: class F { typedef void (Function::*MathFuncPtr)(); std::map<std::string, MathFuncPtr> predefinedFunctions; // lots of other stuff void makeDictionary(){ predefinedFunctions=&F::f_sin(); } };
8
6564
by: Kelly Mandrake | last post by:
I have been learning about STL's map class. I see that I can clear a hashmap by useing the method clear, however I decided to try to clear the hash with a for loop. Since erase can take an iterator as a position I thought I could do something like this: hashTable::iterator i; for (i = phoneBook.begin(); i != phoneBook.end(); i++) { cout << i->first << endl;
17
7545
by: Gernot Frisch | last post by:
restart: for (std::map<x,y>::iterator it = m.begin(); it!=m.end(); ++it) { if( it->second.isbad() ) { std::map<x,y>::iterator next = it; ++next; m.erase(it); it=next; // goto restart;
3
3714
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I can't think of a good fix, or even why it broke. The problem In one of my CFormView derived classes I have a member variable of the type...
13
9673
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
2
5375
by: digz | last post by:
Hi, I am trying to write a program which has two threads one of them write to a map , and the other one deletes entries based on a certain criterion.. first I cannot get the delete portion to work , what am i missing here. also is it possible/correct that the removeKeyValue function acquire the mutex lock only during the call to map.erase(), and not the during the whole iteration process as i have done here( i logically felt it was...
3
2059
by: digz | last post by:
This is a very simplified version of something I am trying to understand. The State object holds the strings and maps , and I pass a reference to State to the process Function which manipulates it based on the tag passed in. I cannot pass the strings/maps in question as references coz .. a) thats redundant, state has everything, it increases number of parameters unnecessarily b) I have to do the case/switch outside the function to...
0
8840
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9131
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9064
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8007
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3189
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.