Gcc misunderstanding vector<const T>??

H

hn.ft.pris

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 int> vec;
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<const 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.
 
I

Ivan Vecerina

: 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 int> vec;
: 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<const 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 T> is illegal in C++: the element type is required
to be assignable, which const T obviously is not.

You should use "vector<T> const" when you need a constant vector.


hth -Ivan
 
G

Gregor Kopp

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<int> vec;
const int a = 1;
vec.push_back(a);
cout << vec[0] << endl;
vec[0] = 2;
cout << vec[0] << endl;
return EXIT_SUCCESS;
}
 
P

P.J. Plauger

: 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 int> vec;
: 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<const 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 T> is 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<T> const" when you need a constant vector.

Agreed.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
H

hn.ft.pris

: 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 int> vec;
: 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<const 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 T> is 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<T> const" when you need a constant vector.Agreed.
Thanks for the explanation. BTW, there is no difference between a
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top