473,572 Members | 3,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_cop y_if(bigMap.beg in(),
bigMap.end(),
std::inserter(l ittleMap, 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 3411
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_cop y_if(bigMap.beg in(),
bigMap.end(),
std::inserter(l ittleMap, 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*******@rocke tmail.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_typ e instead, and that the Assignable
requirement applies to map::mapped_typ e, 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_typ e instead, and that the Assignable
requirement applies to map::mapped_typ e, 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
3605
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 you code a wrapper? I've produced the following: #include <map> template < class keytype, class recordtype > class pseudomap {
19
6127
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 answered some place else (I've searched but not found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible...
1
6461
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 but not under Debug This program is developed under Visual Studio .NET 2003 in a Win32 Console Project // VectorInsert.cpp : Defines the entry point for the console application / #include...
1
1756
by: Paul Dubuc | last post by:
Question: In the following template function signature template <class MAP> int matchAbbreviationMap( const MAP & choices, typename MAP::value_type::first_type & arg, typename MAP::value_type::second_type & val) { ...
2
2835
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)? http_form.h: (sorry about the line wrapping, email client problem) #ifndef HTTP_FORM_H #define HTTP_FORM_H #include <map> #include <string> #include <iostream>
7
6249
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 console for a MUD server I have written. The basic theory is we wrap the functions we want exposed to the console in a function with a prototype of int func(State*)
2
5138
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) ). Is there a way I could specify the entries at the beginning. Is there an implementation-independent hint that I could give to map so that it performs better when I add more entries to it.
7
7156
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 name matches. I defined my predicate like this: class NameMatch {
7
10226
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 for the key, but for whole ranges. Example: I want to be able to tell the container to return object a for every given key between 0 and 10, object c for every key between 11 and 500000 and...
0
7733
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...
0
7640
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7705
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...
0
8000
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...
0
6337
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...
1
5524
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...
0
3685
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2138
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

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.