Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #13520

Re: static_cast and std::vector

From john <gh14tq5@yahoo.com>
Newsgroups comp.lang.c++
Subject Re: static_cast and std::vector
Date 2012-01-20 10:28 -0500
Organization Aioe.org NNTP Server
Message-ID <jfc17g$euc$1@speranza.aioe.org> (permalink)
References <jfbrpt$vd$1@speranza.aioe.org> <IIidnQOEtaTr7oTSnZ2dnUVZ8rydnZ2d@giganews.com> <jfbveg$ae0$1@speranza.aioe.org> <A4mdnUVc_IZqHITSnZ2dnUVZ8tqdnZ2d@giganews.com>

Show all headers | View raw


>>>
>>> Both the C-style cast and the static_cast are wrong; you cannot cast a
>>> std::vector<base*> to a std::vector<derived*>.
>>>
>>> Instead have these getter functions return a std::vector<base*> and
>>> downcast (using static_cast or dynamic_cast) the vector's elements
>>> instead elsewhere in your code.
>>>
>>> /Leigh
>>
>>
>> I do not see why the C-style cast is wrong. It works flawlessly across a
>> wide range of compilers and systems. I can understand the problem with
>> C++ static_cast, but I was hoping there was a way around it. If there is
>> not a valid C++ way to do this, I'll leave it as is.
>
> Just because something "works" does not means that it is "correct". The
> language allows you to perform the C style cast (or reinterpret_cast)
> however *using* the resultant object reference by invoking a std::vector
> member function results in undefined behaviour. Undefined behaviour can
> "silently work" on a particular implementation but that does not mean it
> is correct; it isn't, it is a bug. The basic reason why this is the case
> here is due to the fact that std::vector<base*> and
> std::vector<derived*> are unrelated types.
>

Ok.  Thanks for the explanation.  I may look into fixing this according 
to your suggestion.

John

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

static_cast and std::vector john <gh14tq5@yahoo.com> - 2012-01-20 08:56 -0500
  Re: static_cast and std::vector Leigh Johnston <leigh@i42.co.uk> - 2012-01-20 14:00 +0000
    Re: static_cast and std::vector john <gh14tq5@yahoo.com> - 2012-01-20 09:58 -0500
      Re: static_cast and std::vector Leigh Johnston <leigh@i42.co.uk> - 2012-01-20 15:02 +0000
        Re: static_cast and std::vector john <gh14tq5@yahoo.com> - 2012-01-20 10:28 -0500
      Re: static_cast and std::vector "Fred Zwarts \(KVI\)" <F.Zwarts@KVI.nl> - 2012-01-20 16:48 +0100
        Re: static_cast and std::vector Leigh Johnston <leigh@i42.co.uk> - 2012-01-20 16:02 +0000
          Re: static_cast and std::vector Joshua Maurice <joshuamaurice@gmail.com> - 2012-01-20 14:30 -0800
      Re: static_cast and std::vector Juha Nieminen <nospam@thanks.invalid> - 2012-01-20 19:04 +0000
  Re: static_cast and std::vector Tobias Müller <troplin@bluewin.ch> - 2012-01-21 08:56 +0000
    Re: static_cast and std::vector john <gh14tq5@yahoo.com> - 2012-01-23 12:47 -0500
      Re: static_cast and std::vector Ian Collins <ian-news@hotmail.com> - 2012-01-24 09:22 +1300
  Re: static_cast and std::vector Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2012-01-21 23:02 -0500

csiph-web