std::vector<const MyType> not allowed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • muzicmakr@yahoo.com

    std::vector<const MyType> not allowed

    I'm porting some code from windows to mac, and there are some
    instances of std::vector<con st 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
  • Victor Bazarov

    #2
    Re: std::vector&lt; const MyType&gt; not allowed

    muzicmakr@yahoo .com wrote:
    I'm porting some code from windows to mac, and there are some
    instances of std::vector<con st 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.)
    The general requirement is for the stored type to be assignable and
    copy-constructible. But there can be cases where the assignable
    requirement can be lifted. I can't think of one right now, but it is
    not outside of the realm of possibility... For example, if you use the form

    std::vector<myt ypev(10);

    to create a vector of 10 objects, your 'mytype' should also be
    default-constructible. But if you use the form

    mytype prototype(somea rguments);
    std::vector<myt ypev(10, prototype);

    there is no requirement for default-constructibilit y...

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask

    Comment

    • Sam

      #3
      Re: std::vector&lt; const MyType&gt; not allowed

      muzicmakr@yahoo .com writes:
      I'm porting some code from windows to mac, and there are some
      instances of std::vector<con st 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.
      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<MyT ype>, 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<MyT ype&. 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
      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.4.9 (GNU/Linux)

      iEYEABECAAYFAki gyhMACgkQx9p3GY HlUOK9vACdGVEZA 9MccaIz/azyhWgNhVxR
      y/0An07qbB7Ni4cwZ VnwYU1ZXR4nxSb7
      =vSnM
      -----END PGP SIGNATURE-----

      Comment

      • James Kanze

        #4
        Re: std::vector&lt; const MyType&gt; not allowed

        On Aug 12, 12:08 am, muzicm...@yahoo .com wrote:
        I'm porting some code from windows to mac, and there are some
        instances of std::vector<con st 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:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        • Lance Diduck

          #5
          Re: std::vector&lt; const MyType&gt; not allowed

          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

          Comment

          • muzicmakr@yahoo.com

            #6
            Re: std::vector&lt; const MyType&gt; not allowed

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

            mich

            Comment

            • pjp@plauger.com

              #7
              Re: std::vector&lt; const MyType&gt; not allowed

              On Aug 11, 7:24 pm, Sam <s...@email-scan.comwrote:
               application_pgp-signature_part
              < 1KViewDownload
              >
              muzicm...@yahoo .com writes:
              I'm porting some code from windows to mac, and there are some
              instances of std::vector<con st 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.

                          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<MyT ype>, 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<MyT ype&. 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 -

              Comment

              Working...