473,857 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const element type in standard library containers

Program 1:
---------------
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
vector<const intc2;

return EXIT_SUCCESS;
}

This program gives compilation error for the vector instantiation.

Now consider the following program 2:
-------------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <set>
#include <utility>

using namespace std;

int main()
{
typedef pair<const int, const intp_type;

multiset<p_type c1;

c1.insert(make_ pair(0, 2));

return EXIT_SUCCESS;
}

But this second program also has 'const int' as part of the element
type; but it compiles fine.

Why doesn't the first program compile and why does the second program
compile fine ?

Kindly clarify.

Thanks
V.Subramanian
Jun 27 '08 #1
16 2075
Consider the program z.cpp:

#include <cstdlib>
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <map>
#include <utility>

using namespace std;

int main()
{
typedef pair<const int, const intp_type;

vector<p_typev;
//v.insert(v.begi n(), make_pair(0, 2));

list<p_typel;
l.insert(l.begi n(), make_pair(0, 2));

deque<p_typed;
//d.insert(d.begi n(), make_pair(0, 2));

map<const int, const ints;
s.insert(make_p air(0, 2));

return EXIT_SUCCESS;
}

I am using
g++ -std=c++98 -pedantic -Wall -Wextra z.cpp

Here if I remove the two comments(one each on vector insert and deque
insert), I get compilation error. But for list, there is no
compilation error for a similar insert. Why does compilation error
occur, only for vector and deque and not for list and map ?

Kindly clarify.

Thanks
V.Subramanian
Jun 27 '08 #2
su************* *@yahoo.com wrote:
Consider the program z.cpp:

#include <cstdlib>
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <map>
#include <utility>

using namespace std;

int main()
{
typedef pair<const int, const intp_type;

vector<p_typev;
//v.insert(v.begi n(), make_pair(0, 2));

list<p_typel;
l.insert(l.begi n(), make_pair(0, 2));

deque<p_typed;
//d.insert(d.begi n(), make_pair(0, 2));

map<const int, const ints;
s.insert(make_p air(0, 2));

return EXIT_SUCCESS;
}

I am using
g++ -std=c++98 -pedantic -Wall -Wextra z.cpp

Here if I remove the two comments(one each on vector insert and
deque insert), I get compilation error. But for list, there is no
compilation error for a similar insert. Why does compilation error
occur, only for vector and deque and not for list and map ?
Standard containers require that the value type is assignable. When
the pair members are const, it is not.

With list and map, you just get away with this violation when the
assignment operator isn't used by the implementation.
Bo Persson
Jun 27 '08 #3
On May 28, 1:51 pm, "Bo Persson" <b...@gmb.dkwro te:
subramanian10.. .@yahoo.com wrote:
Consider the program z.cpp:
#include <cstdlib>
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <map>
#include <utility>
using namespace std;
int main()
{
typedef pair<const int, const intp_type;
vector<p_typev;
//v.insert(v.begi n(), make_pair(0, 2));
list<p_typel;
l.insert(l.begi n(), make_pair(0, 2));
deque<p_typed;
//d.insert(d.begi n(), make_pair(0, 2));
map<const int, const ints;
s.insert(make_p air(0, 2));
return EXIT_SUCCESS;
}
I am using
g++ -std=c++98 -pedantic -Wall -Wextra z.cpp
Here if I remove the two comments(one each on vector insert and
deque insert), I get compilation error. But for list, there is no
compilation error for a similar insert. Why does compilation error
occur, only for vector and deque and not for list and map ?

Standard containers require that the value type is assignable. When
the pair members are const, it is not.

With list and map, you just get away with this violation when the
assignment operator isn't used by the implementation.

Bo Persson
Does this mean that the CopyConstructib le and Assignable requirements
are not mandatory for list, map ? Rather, are they implementation-
dependent ?

Kindly clarify.

Thanks
V.Subramanian
Jun 27 '08 #4
su************* *@yahoo.com, India wrote:
On May 28, 1:51 pm, "Bo Persson" <b...@gmb.dkwro te:
>Standard containers require that the value type is assignable. When
the pair members are const, it is not.

With list and map, you just get away with this violation when the
assignment operator isn't used by the implementation.

Bo Persson

Does this mean that the CopyConstructib le and Assignable requirements
are not mandatory for list, map ? Rather, are they implementation-
dependent ?
No, you get away with it because the insert operation isn't doing an
assignment.

--
Ian Collins.
Jun 27 '08 #5
On May 29, 8:42 am, Ian Collins <ian-n...@hotmail.co mwrote:
subramanian10.. .@yahoo.com, India wrote:
On May 28, 1:51 pm, "Bo Persson" <b...@gmb.dkwro te:
Standard containers require that the value type is
assignable. When the pair members are const, it is not.
With list and map, you just get away with this violation
when the assignment operator isn't used by the
implementation.
Does this mean that the CopyConstructib le and Assignable
requirements are not mandatory for list, map ? Rather, are
they implementation- dependent ?
No, you get away with it because the insert operation isn't
doing an assignment.
You don't get away with anything. Violating the requirements is
undefined behavior. An implementation is not required to
diagnose it, but it isn't required to document what happens if
there is no diagnostic. The code is wrong, and should be
corrected, even if your tests today don't show any problems.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #6
James Kanze wrote:
On May 29, 8:42 am, Ian Collins <ian-n...@hotmail.co mwrote:
>subramanian10. ..@yahoo.com, India wrote:
>>On May 28, 1:51 pm, "Bo Persson" <b...@gmb.dkwro te:
Standard containers require that the value type is
assignable . When the pair members are const, it is not.
>>>With list and map, you just get away with this violation
when the assignment operator isn't used by the
implementati on.
>>Does this mean that the CopyConstructib le and Assignable
requirement s are not mandatory for list, map ? Rather, are
they implementation- dependent ?
>No, you get away with it because the insert operation isn't
doing an assignment.

You don't get away with anything. Violating the requirements is
undefined behavior. An implementation is not required to
diagnose it, but it isn't required to document what happens if
there is no diagnostic. The code is wrong, and should be
corrected, even if your tests today don't show any problems.
I think that's what I said.....

--
Ian Collins.
Jun 27 '08 #7
On May 29, 12:55 pm, Ian Collins <ian-n...@hotmail.co mwrote:
James Kanze wrote:
On May 29, 8:42 am, Ian Collins <ian-n...@hotmail.co mwrote:
subramanian10.. .@yahoo.com, India wrote:
On May 28, 1:51 pm, "Bo Persson" <b...@gmb.dkwro te:
Standard containers require that the value type is
assignable. When the pair members are const, it is not.
>>With list and map, you just get away with this violation
when the assignment operator isn't used by the
implementatio n.
>Does this mean that the CopyConstructib le and Assignable
requirements are not mandatory for list, map ? Rather, are
they implementation- dependent ?
No, you get away with it because the insert operation isn't
doing an assignment.
You don't get away with anything. Violating the requirements is
undefined behavior. An implementation is not required to
diagnose it, but it isn't required to document what happens if
there is no diagnostic. The code is wrong, and should be
corrected, even if your tests today don't show any problems.
I think that's what I said.....
You said that even if the tests show that the code works...:-)

Seriously, I think that's what you meant. But the way you
expressed it could easily be misunderstood by someone whose
native language wasn't English; it depended on a certain reading
between the lines. And I've had enough experience with
non-native English speakers to know that it helps to put the
dots on the i's (which I'm not sure that non-native speakers
will understand either).

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #8
James Kanze wrote:
On May 29, 12:55 pm, Ian Collins <ian-n...@hotmail.co mwrote:
>James Kanze wrote:
>>On May 29, 8:42 am, Ian Collins <ian-n...@hotmail.co mwrote:
subramanian1 0...@yahoo.com, India wrote:
On May 28, 1:51 pm, "Bo Persson" <b...@gmb.dkwro te:
>Standard containers require that the value type is
>assignable . When the pair members are const, it is not.
>>>>>With list and map, you just get away with this violation
>when the assignment operator isn't used by the
>implementa tion.
>>>>Does this mean that the CopyConstructib le and Assignable
requirement s are not mandatory for list, map ? Rather, are
they implementation- dependent ?
>>>No, you get away with it because the insert operation isn't
doing an assignment.
>>You don't get away with anything. Violating the requirements is
undefined behavior. An implementation is not required to
diagnose it, but it isn't required to document what happens if
there is no diagnostic. The code is wrong, and should be
corrected, even if your tests today don't show any problems.
>I think that's what I said.....

You said that even if the tests show that the code works...:-)
Ah but I would have added a test for insert of an immutable object!

--
Ian Collins.
Jun 27 '08 #9
On May 30, 6:27 am, Ian Collins <ian-n...@hotmail.co mwrote:
>>No, you get away with it because the insert operation isn't
doing an assignment.
>You don't get away with anything. Violating the requirements is
undefined behavior. An implementation is not required to
diagnose it, but it isn't required to document what happens if
there is no diagnostic. The code is wrong, and should be
corrected, even if your tests today don't show any problems.
I think that's what I said.....
You said that even if the tests show that the code works...:-)
Ah but I would have added a test for insert of an immutable
object!
Which might pass, even though the code is wrong. Undefined
behavior means that tests are meaningless, and IMHO, the problem
*isn't* with the concept of testing.

The "meaningles s" is obviously hyperbole. But the presence of
undefined behavior in C++ does make reliable software
significantly more difficult, since it reduces the significence
of tests: you can never be sure that the test didn't pass just
because some undefined behavior happened to work this time.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #10

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

Similar topics

1
2235
by: Wolfgang Lipp | last post by:
my question is: do we need container elements for repeating elements in data-centric xml documents? or is it for some reason very advisable to introduce containers in xml documents even where not strictly needed? how can a recommendation on this in the light of existing tools like w3c xml schema and relaxng as well es established practice be answered? i would greatly appreciate any words, pointers, and links. the exposition of the...
0
625
by: Wolfgang Lipp | last post by:
From: Lipp, Wolfgang Sent: Tuesday, 27?January?2004 13:26 <annotation> the first eleven contributions in this thread started as an off-list email discussion; i have posted them here with the consent of their authors. </annotation>
9
3167
by: John Calcote | last post by:
I'm not sure is there's a g++ specific newsgroup... g++ is telling me there's a const-related problem with my source code and I just don't see it. Now, I don't know that it means much, but VC7 doesn't see the problem either: ----snip-snip-snip---- #include <string> #include <set>
12
1941
by: Christof Krueger | last post by:
Hello, I'm quite new to C++ so maybe there's something I miss. I write a simple board game. It has a board class. This class has a method that returns the count of pieces a player has on the board. Since this function does not change anything in the class I declared it as const. To count all pieces of a given color the functions iterates through a "map" of CNode-pointers. "CNode" is another class that is irrelevant to the problem.
7
2237
by: BigMan | last post by:
Which is the preferred way to create a const container in general: 1. container_type< value_type > const; or 2. container_type< value_type const >?
43
5047
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own libraries. I'm not sure if that's a good thing or not. AFAIK, most of Qt is compatable with the Standard Library. That is, QLT can interoperate with STL, and you can convert back and forth between std::string and Qt::QString, etc. Are there any...
7
4371
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
2
2022
by: Lorenzo Castelli | last post by:
This is an old problem of mine. Basically I have an abstract base class which represents a generic iterator over a collection of elements, and various derived classes that implement the traversing on specific data structures. For each iterator I want to be able to specify the four possible const combinations, corresponding to the various void* for pointers, with the possibility to perform conversions from non-const to const just like...
0
1879
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle, and you'll be left with just noisy compiler warnings and confusion. if you start a project with all char *, and char ** and even char ***, if you begin at the low level weeding out references of 'passing const char * to char * ( such as...
0
9923
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
9768
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11083
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
10808
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
10394
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...
1
7940
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
7104
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4592
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
4191
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.