Gcc misunderstanding vector<const T>??

Discussion in 'C++' started by hn.ft.pris@gmail.com, Jan 23, 2007.

  1. Guest

    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.
     
    , Jan 23, 2007
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    : 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
     
    Ivan Vecerina, Jan 23, 2007
    #2
    1. Advertising

  3. Gregor Kopp Guest

    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;
    }
     
    Gregor Kopp, Jan 23, 2007
    #3
  4. P.J. Plauger Guest

    "Ivan Vecerina" <> wrote in message
    news:35900$45b5cc0f$55da16d7$...

    > <> wrote in message
    > news:...
    > : 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
     
    P.J. Plauger, Jan 23, 2007
    #4
  5. Guest

    On Jan 23, 5:53 pm, "P.J. Plauger" <> wrote:
    > "Ivan Vecerina" <> wrote in messagenews:35900$45b5cc0f$55da16d7$...
    >
    >
    >
    >
    >
    > > <> wrote in message
    > >news:...
    > > : 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 -
     
    , Jan 24, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    1,989
    Csaba
    Feb 18, 2006
  2. Replies:
    11
    Views:
    1,143
  3. Javier
    Replies:
    2
    Views:
    604
    James Kanze
    Sep 4, 2007
  4. 0m
    Replies:
    26
    Views:
    1,159
    Tim Rentsch
    Nov 10, 2008
  5. Replies:
    2
    Views:
    552
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page