Re: std::vector<const MyType> 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<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:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34