473,721 Members | 2,186 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Gcc misunderstandin g vector<const T>??

Hi:
I've got the following simple code to test vector<const T>,

#include <vector>
#include <iostream>

using namespace std;

int main( void ){
vector<const intvec;
const int a = 1;

vec.push_back(a );
cout << vec[0] << endl; // Output 1
vec[0] = 2;
cout << vec[0] << endl; // Output 2
return 1;
}

The code passes compilation on MS VC8, but the output is incorrect, for
the data member of container is const int, so it shouldn't be modified.
I guess there is nothing different between a "vector<int >" and
"vector<con st int>" in MS VC8.

But above code fails on GCC 3.4.2. The compiler complains that there is
"assignment of read-only location", why does it happens, what shall I
do if I want to manipulate vector <const T>? Thanks for help.

Jan 23 '07 #1
4 2787
<hn********@gma il.comwrote in message
news:11******** *************@5 1g2000cwl.googl egroups.com...
: Hi:
: I've got the following simple code to test vector<const T>,
:
: #include <vector>
: #include <iostream>
:
: using namespace std;
:
: int main( void ){
: vector<const intvec;
: const int a = 1;
:
: vec.push_back(a );
: cout << vec[0] << endl; // Output 1
: vec[0] = 2;
: cout << vec[0] << endl; // Output 2
: return 1;
: }
:
: The code passes compilation on MS VC8, but the output is incorrect,
for
: the data member of container is const int, so it shouldn't be
modified.
: I guess there is nothing different between a "vector<int >" and
: "vector<con st int>" in MS VC8.
:
: But above code fails on GCC 3.4.2. The compiler complains that there
is
: "assignment of read-only location", why does it happens, what shall I
: do if I want to manipulate vector <const T>? Thanks for help.

GCC is correct. MSVC8 misbehaves.
Using vector<const Tis illegal in C++: the element type is required
to be assignable, which const T obviously is not.

You should use "vector<Tco nst" when you need a constant vector.
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jan 23 '07 #2
hn********@gmai l.com schrieb:
>
But above code fails on GCC 3.4.2. The compiler complains that there is
"assignment of read-only location", why does it happens, what shall I
do if I want to manipulate vector <const T>? Thanks for help.
What do you want to archieve with vector <const T>? I do not see
adavantages here, but I want to learn.
A vector have to get the possibility to change the values inside I
think. That works:

#include <vector>
#include <iostream>

using namespace std;

int main(void) {
vector<intvec;
const int a = 1;
vec.push_back(a );
cout << vec[0] << endl;
vec[0] = 2;
cout << vec[0] << endl;
return EXIT_SUCCESS;
}
Jan 23 '07 #3
"Ivan Vecerina" <_I************ *******@ivan.ve cerina.comwrote in message
news:35******** *************** ****@news.hispe ed.ch...
<hn********@gma il.comwrote in message
news:11******** *************@5 1g2000cwl.googl egroups.com...
: Hi:
: I've got the following simple code to test vector<const T>,
:
: #include <vector>
: #include <iostream>
:
: using namespace std;
:
: int main( void ){
: vector<const intvec;
: const int a = 1;
:
: vec.push_back(a );
: cout << vec[0] << endl; // Output 1
: vec[0] = 2;
: cout << vec[0] << endl; // Output 2
: return 1;
: }
:
: The code passes compilation on MS VC8, but the output is incorrect,
for
: the data member of container is const int, so it shouldn't be
modified.
: I guess there is nothing different between a "vector<int >" and
: "vector<con st int>" in MS VC8.
:
: But above code fails on GCC 3.4.2. The compiler complains that there
is
: "assignment of read-only location", why does it happens, what shall I
: do if I want to manipulate vector <const T>? Thanks for help.

GCC is correct. MSVC8 misbehaves.
No. The program is ill formed, so implementations have a bit of latitude.
We have enough requests from customers who wanted to instantiate
containers on const types that we added const stripping some time back.
Dunno why they want it, but they do. So we gave it to 'em.
Using vector<const Tis illegal in C++: the element type is required
to be assignable, which const T obviously is not.
Governments make things illegal. Standards make things invalid. There's
no law against giving meaning to this particular invalid program.
You should use "vector<Tco nst" when you need a constant vector.
Agreed.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jan 23 '07 #4


On Jan 23, 5:53 pm, "P.J. Plauger" <p...@dinkumwar e.comwrote:
"Ivan Vecerina" <_INVALID_use_w ebfo...@ivan.ve cerina.comwrote in messagenews:35* *************** ***********@new s.hispeed.ch...


<hn.ft.p...@gma il.comwrote in message
news:11******** *************@5 1g2000cwl.googl egroups.com...
: Hi:
: I've got the following simple code to test vector<const T>,
:
: #include <vector>
: #include <iostream>
:
: using namespace std;
:
: int main( void ){
: vector<const intvec;
: const int a = 1;
:
: vec.push_back(a );
: cout << vec[0] << endl; // Output 1
: vec[0] = 2;
: cout << vec[0] << endl; // Output 2
: return 1;
: }
:
: The code passes compilation on MS VC8, but the output is incorrect,
for
: the data member of container is const int, so it shouldn't be
modified.
: I guess there is nothing different between a "vector<int >" and
: "vector<con st int>" in MS VC8.
:
: But above code fails on GCC 3.4.2. The compiler complains that there
is
: "assignment of read-only location", why does it happens, what shall I
: do if I want to manipulate vector <const T>? Thanks for help.
GCC is correct. MSVC8 misbehaves.No. The program is ill formed, so implementations have a bit of latitude.
We have enough requests from customers who wanted to instantiate
containers on const types that we added const stripping some time back.
Dunno why they want it, but they do. So we gave it to 'em.
Using vector<const Tis illegal in C++: the element type is required
to be assignable, which const T obviously is not.Governments make things illegal. Standards make things invalid. There's
no law against giving meaning to this particular invalid program.
You should use "vector<Tco nst" when you need a constant vector.Agreed.
Thanks for the explanation. BTW, there is no difference between a
"vector<Tco nst" and "const vector<T>", right?
P.J. Plauger
Dinkumware, Ltd.http://www.dinkumware.com- Hide quoted text -- Show quoted text -
Jan 24 '07 #5

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

Similar topics

8
4873
by: Joseph Turian | last post by:
Some function requires a vector<const foo*> argument. How can I cast a vector<foo*> to vector<const foo*>? Thanks! Joseph
1
4905
by: Shafik | last post by:
Hello folks, Whats the reason for the STL not allowing this: vector<const stringv = vector<const string>(); --Shafik
1
4576
by: Javier | last post by:
Hello, thanks for the replies to my questions. I have one more: class A { public: m1() const; m2(); };
9
2244
by: t | last post by:
Can you use a plain iterator to vector<const intto write to it? It doesn't seem like it should be possible, but Visual C++ 2005 Express lets me: vector<const intvc; vc.push_back(5); vector<const int>::iterator iter = vc.begin(); *iter = 3; // compiles
6
3430
by: muzicmakr | last post by:
I'm porting some code from windows to mac, and there are some instances of std::vector<const MyType>, that compiled just fine on the pc, but won't compile under gcc. I'd never tried to do this particular construct myself, and after some searching online, I found a post somewhere saying this isn't legal c++ because the standard containers need types that are assignable. My questions-- is that correct? Why does visual studio allow it if...
12
5681
by: eiji.anonremail | last post by:
Hi all, I'm facing some uncertainty with const template arguments. Maybe someone could explain the general strategy. #include <vector> int main(int arc, char** argv) {
0
8730
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
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...
0
9215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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...
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
5981
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();...
0
4753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.