Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::vector<const MyType> not allowed

Reply
Thread Tools

std::vector<const MyType> not allowed

 
 
muzicmakr@yahoo.com
Guest
Posts: n/a
 
      08-11-2008
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
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      08-12-2008
On Aug 12, 12:08 am, (E-Mail Removed) 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:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
 
Lance Diduck
Guest
Posts: n/a
 
      08-12-2008
> 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

 
Reply With Quote
 
muzicmakr@yahoo.com
Guest
Posts: n/a
 
      08-12-2008
Thanks for all the informative replies, they were very helpful.

mich
 
Reply With Quote
 
pjp@plauger.com
Guest
Posts: n/a
 
      08-12-2008
On Aug 11, 7:24*pm, Sam <(E-Mail Removed)> wrote:
> *application_pgp-signature_part
> < 1KViewDownload
>
> (E-Mail Removed) 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 -


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why defining a constant in a method is not allowed but usingself.class.const_set is allowed? Iñaki Baz Castillo Ruby 13 05-01-2011 06:09 PM
1.Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Sound Javascript 2 09-28-2006 02:43 PM
(405) Method Not Allowed when uploading a file to a web site Rocio ASP .Net 0 04-05-2004 03:10 PM
Reading registry key throws "Requested registry access is not allowed." HK ASP .Net 1 04-01-2004 04:44 PM
Requested registry access is not allowed Gary ASP .Net 2 01-17-2004 07:34 AM



Advertisments