std::vector<const MyType> not allowed

Discussion in 'C++' started by muzicmakr@yahoo.com, Aug 11, 2008.

  1. Guest

    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
    it's wrong? And is there some other way to try to preserve the intent
    of the original? (Otherwise I'm just going to strip out the
    consts.)

    Thanks for your time!

    mich
    , Aug 11, 2008
    #1
    1. Advertising

  2. James Kanze Guest

    On Aug 12, 12:08 am, wrote:
    > 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 it's wrong?


    It's undefined behavior. It may cause an error when compiling,
    it may compile, but do strange things during execution, or it
    may work just fine.

    > And is there some other way to try to preserve the intent of
    > the original? (Otherwise I'm just going to strip out the
    > consts.)


    What is the intent of the original? Given the semantics of
    vector, having a non-const vector of const objects doesn't make
    much sense. And if you declare an std::vector< MyType > const,
    there's no way you can get a non-const reference to any of the
    elements.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Aug 12, 2008
    #2
    1. Advertising

  3. Lance Diduck Guest

    > My questions-- is that correct?  Why does visual studio allow it if
    > it's wrong?  

    MSVC probably allowed it because your code never uses a member
    function that cared whether T was const or not. This is an example of
    a struct that in one version allows a const whereas the other does not
    template<class T>
    struct Foo{
    Foo(){}
    #ifdef ALLOW_CONST_T
    Foo(T const&v):var(v){}
    #else
    Foo(T const&v){
    var=v;
    }
    #endif
    T var;
    };

    Whether I define ALLOW_CONST_T or not does not change the behavior of
    Foo(other than performance in certian cases of T) but one version
    allows Foo<const X> and the other does not. And that only applies if
    the user of Foo actually invokes that particular constructor.
    So what is likely happening is that the particular mixture of user
    code and how the container was implemented never actully hit upon
    something that cared about the constness of T, whereas in the gcc
    version it did.
    Lance
    Lance Diduck, Aug 12, 2008
    #3
  4. Guest

    Thanks for all the informative replies, they were very helpful.

    mich
    , Aug 12, 2008
    #4
  5. Guest

    On Aug 11, 7:24 pm, Sam <> wrote:
    >  application_pgp-signature_part
    > < 1KViewDownload
    >
    > writes:
    > > 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?

    >
    > That is correct.
    >
    > >                                  Why does visual studio allow it if
    > > it's wrong?

    >
    > Because the very last thing that Microsoft wants you to do is write portable
    > code that complies with commodity standards that are not controlled by
    > Microsoft. Microsoft would like nothing more than to force you to write code
    > that compiles only on Windows.


    Nice guess, if a bit paranoid, but you're wrong. Dinkumware added the
    capability to declare a container of const type because -- are you
    ready for this? -- customers demanded it. And now the latest revision
    of the C++ Standard calls for *all* implementations to support vectors
    of const type for all operations that make sense.

    So much for vendor lock in.

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

    > >             And is there some other way to try to preserve the intent
    > > of the original?  (Otherwise I'm just going to strip out the
    > > consts.)

    >
    > The "intent of the original" would be to declare the array as a plain,
    > garden-variety std::vector<MyType>, and, in functions or a context that
    > should not be able to modify the contents of the vector, pass it by
    > reference as a const std::vector<MyType> &. Any code will have to access the
    > vector using const_iterator, not iterator (you'll probably have to rewrite a
    > whole bunch of stuff).
    >
    >
    >
    >
    >
    > > Thanks for your time!

    >
    > > mich- Hide quoted text -

    >
    > - Show quoted text -
    , Aug 12, 2008
    #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. Lynn
    Replies:
    11
    Views:
    645
    Default User
    Dec 15, 2004
  2. Geoffrey S. Knauth
    Replies:
    6
    Views:
    983
    Earl Purple
    Jan 18, 2006
  3. Javier
    Replies:
    2
    Views:
    549
    James Kanze
    Sep 4, 2007
  4. Replies:
    2
    Views:
    531
    Andrew Koenig
    Feb 9, 2009
  5. daniele.g
    Replies:
    4
    Views:
    476
    Joe Pfeiffer
    Jul 15, 2011
Loading...

Share This Page