472,325 Members | 1,195 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,325 software developers and data experts.

Standard 23.1 - std::map::value_type must be assignable?

I'm puzzled about part of the standard. 23.1 states that items stored
in a container must be assignable. Therefore, the items in a map--that
is, std::pair<const Key, valuemust be assignable. However, such a
pair--with the const Key--is not assignable.

My concern is more than academic; take for instance the following code
to make a copy of a map while eliminating some of the elements:

#include <map>
#include <algorithm>

typedef std::map<char, intMapType;

bool predicate(const MapType::value_type& x)
{
return x.second 1;
}

int main()
{
MapType bigMap;
bigMap['a'] = 1;
bigMap['b'] = 2;

MapType littleMap;

// This shouldn't work, right? Standard 23.1 says objects in
// container must be assignable, but MapType::value_type is
// std::pair<const char, intwhich is NOT assignable
std::remove_copy_if(bigMap.begin(),
bigMap.end(),
std::inserter(littleMap, littleMap.end()),
&predicate);
return 0;
}

This compiles fine on g++ 4.1.2, but I want to make sure I'm not
writing nonstandard code. That leaves me with two questions:

1) is there part of the standard that I'm missing that describes how
maps can have values of std::pair<const Key, valueeven though that
pair is not assignable?

2) is the sample code above standard compliant, and if not, how should
I accomplish something like this?

Thanks,
Omari
Dec 31 '07 #1
3 3222
massysett wrote:
I'm puzzled about part of the standard. 23.1 states that items stored
in a container must be assignable. Therefore, the items in a map--that
is, std::pair<const Key, valuemust be assignable. However, such a
pair--with the const Key--is not assignable.

My concern is more than academic; take for instance the following code
to make a copy of a map while eliminating some of the elements:

#include <map>
#include <algorithm>

typedef std::map<char, intMapType;

bool predicate(const MapType::value_type& x)
{
return x.second 1;
}

int main()
{
MapType bigMap;
bigMap['a'] = 1;
bigMap['b'] = 2;

MapType littleMap;

// This shouldn't work, right? Standard 23.1 says objects in
// container must be assignable, but MapType::value_type is
// std::pair<const char, intwhich is NOT assignable
std::remove_copy_if(bigMap.begin(),
bigMap.end(),
std::inserter(littleMap, littleMap.end()),
&predicate);
return 0;
}

This compiles fine on g++ 4.1.2, but I want to make sure I'm not
writing nonstandard code. That leaves me with two questions:

1) is there part of the standard that I'm missing that describes how
maps can have values of std::pair<const Key, valueeven though that
pair is not assignable?

2) is the sample code above standard compliant, and if not, how should
I accomplish something like this?
Consider assignable as x = y. The thing is that the key and the value must
be assignable, you need an assignment operator, either default or custom.
The reason being that values are loaded into the map using assignment. A
char is assignable ( ignore the constaness for now ) and so is an int.

The map itself does not have to treat the key as const internally, the
interface does. It is extremely easy to convert a non-const something to a
const something, just not as easy the other way around.

Notice the standard says objects in the container. That is refering to the
key and the value, not the std::pair.

--
Jim Langston
ta*******@rocketmail.com
Dec 31 '07 #2
massysett wrote:
I'm puzzled about part of the standard. 23.1 states that items stored
in a container must be assignable. Therefore, the items in a map--that
is, std::pair<const Key, valuemust be assignable. However, such a
pair--with the const Key--is not assignable.
That observation is an important part of defect report 276:

http://www.open-std.org/jtc1/sc22/wg...fects.html#276

The report remarks:

"It should be noted that there exists a valid and non-contradictory
interpretation of the current text. The wording in 23.1/3 avoids
mentioning value_type, referring instead to "objects stored in a
container." One might argue that map does not store objects of type
map::value_type, but of map::mapped_type instead, and that the Assignable
requirement applies to map::mapped_type, not map::value_type.
Best

Kai-Uwe Bux
Dec 31 '07 #3
On Dec 31, 2:41 am, jkherci...@gmx.net wrote:
That observation is an important part of defect report 276:

http://www.open-std.org/jtc1/sc22/wg...fects.html#276

The report remarks:

"It should be noted that there exists a valid and non-contradictory
interpretation of the current text. The wording in 23.1/3 avoids
mentioning value_type, referring instead to "objects stored in a
container." One might argue that map does not store objects of type
map::value_type, but of map::mapped_type instead, and that the Assignable
requirement applies to map::mapped_type, not map::value_type.
I thought of that too, but the argument falls apart. 23.1/5: "X
denotes a container class containing objects of type T." Table 65 says
X::value_type returns T. 23.3.1: "typedef pair <const Key, T>
value_type". pair <const Key, Tis not assignable.

At best the standard is confusing on this point; at worst it is simply
inconsistent.
Dec 31 '07 #4

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

Similar topics

25
by: Christopher Benson-Manica | last post by:
If you liked the functionality of std::map, but found that you couldn't trust your implementation to handle nonstandard data structures, how would...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is...
1
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode...
1
by: Paul Dubuc | last post by:
Question: In the following template function signature template <class MAP> int matchAbbreviationMap( const MAP & choices, typename...
2
by: Fei Liu | last post by:
Hello, I am getting something really wierd out of this source code, can you please verify if you also get segmentation fault (memory failure)? ...
7
by: DevNull | last post by:
Hi there everyone, I'm creating a very simple immediate mode command interpreter. The final purpose is to provide a pluggable control and command...
2
by: nerdrakesh | last post by:
Hi - I am using the map stl and is there a way I could specify the hash table size in the map in the beginning - (similar to vector.reserve(size) ...
7
by: Anonymous | last post by:
I have map variable that maps ids (unsigned int) to objects. Each object has a Name() method. I want to use std::find_if to fetch the object that...
7
by: guido | last post by:
Hi, I'm looking for a container class that can map whole ranges of keys to objects - something like std::map, but not only for individual values...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.