Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Gcc misunderstanding vector<const T>?? (http://www.velocityreviews.com/forums/t459991-gcc-misunderstanding-vector-const-t.html)

hn.ft.pris@gmail.com 01-23-2007 08:33 AM

Gcc misunderstanding 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 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.


Ivan Vecerina 01-23-2007 08:49 AM

Re: Gcc misunderstanding vector<const T>??
 
<hn.ft.pris@gmail.com> wrote in message
news:1169541237.009620.97070@51g2000cwl.googlegrou ps.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 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



Gregor Kopp 01-23-2007 08:59 AM

Re: Gcc misunderstanding vector<const T>??
 
hn.ft.pris@gmail.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<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.J. Plauger 01-23-2007 09:53 AM

Re: Gcc misunderstanding vector<const T>??
 
"Ivan Vecerina" <_INVALID_use_webform_@ivan.vecerina.com> wrote in message
news:35900$45b5cc0f$55da16d7$12135@news.hispeed.ch ...

> <hn.ft.pris@gmail.com> wrote in message
> news:1169541237.009620.97070@51g2000cwl.googlegrou ps.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 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



hn.ft.pris@gmail.com 01-24-2007 05:04 AM

Re: Gcc misunderstanding vector<const T>??
 


On Jan 23, 5:53 pm, "P.J. Plauger" <p...@dinkumware.com> wrote:
> "Ivan Vecerina" <_INVALID_use_webfo...@ivan.vecerina.com> wrote in messagenews:35900$45b5cc0f$55da16d7$12135@news.his peed.ch...
>
>
>
>
>
> > <hn.ft.p...@gmail.com> wrote in message
> >news:1169541237.009620.97070@51g2000cwl.googlegro ups.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 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
"vector<T> const" and "const vector<T>", right?

> P.J. Plauger
> Dinkumware, Ltd.http://www.dinkumware.com- Hide quoted text -- Show quoted text -




All times are GMT. The time now is 06:52 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.