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


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

Re: static_cast and std::vector

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: static_cast and std::vector
Date Tue, 24 Jan 2012 09:22:54 +1300
Lines 59
Message-ID <9o5tsuFbb0U1@mid.individual.net> (permalink)
References <jfbrpt$vd$1@speranza.aioe.org> <953898973348828453.949041troplin-bluewin.ch@news.aioe.org> <jfk6fm$du4$1@speranza.aioe.org>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net 0sIe2G7d5HHCeHN8Tl/08QmvjClj2jaPkj8HKjJs3XAu8uAZTU
Cancel-Lock sha1:vtvEnoHxf4aVrB7chfymtr0Nycs=
User-Agent Mozilla/5.0 (X11; SunOS i86pc; rv:6.0.2) Gecko/20111003 Thunderbird/6.0.2
In-Reply-To <jfk6fm$du4$1@speranza.aioe.org>
Xref x330-a1.tempe.blueboxinc.net comp.lang.c++:13556

Show key headers only | View raw


On 01/24/12 06:47 AM, john wrote:
>>> #include<vector>
>>>
>>> class Base {};
>>> class Derived0 : public Base {};
>>> class Derived1 : public Base {};
>>>
>>> class Y
>>> {
>>>      std::vector<Base*>   m_vb[2];
>>>
>>>      public:
>>>
>>>      void Add(Derived0* d0) { m_vb[0].push_back(d0); }
>>>      void Add(Derived1* d1) { m_vb[1].push_back(d1); }
>>>
>>>      const std::vector<Derived0*>* GetD0()
>>> 	{ return (const std::vector<Derived0*>*)&m_vb[0]; }
>>>      const std::vector<Derived1*>* GetD1()
>>> 	{ return (const std::vector<Derived1*>*)&m_vb[1]; }
>>>
>>>      const std::vector<Derived0*>* GetDD0()
>>> 	{ return static_cast<const std::vector<Derived0*>*>(&m_vb[0]); }
>>>      const std::vector<Derived1*>* GetDD1()
>>> 	{ return static_cast<const std::vector<Derived1*>*>(&m_vb[1]); }
>>>
>>> };
>>
>> Why don't you just write:
>> std::vector<Derived0*>   m_vd0;
>> std::vector<Derived1*>   m_vd1;
>> and use those without any casting?
>>
>> This would be the most obvious solution and you are losing nothing.
>
> We have looked into this, but we also really need functionality like
>
>     const std::vector<Base*>* GetBase(int which)
>    	{ return&m_vb[which]; }
>
> So, changing as you suggested would preclude this.  Would it be possible
> to replace the UB casts
>
>        const std::vector<Derived0*>* GetD0()
>    	{ return (const std::vector<Derived0*>*)&m_vb[0]; }
>        const std::vector<Derived1*>* GetD1()
>    	{ return (const std::vector<Derived1*>*)&m_vb[1]; }
>
> with some type of specialized iterator?

What do you do with the returned vectors?

You don't have a vector of Derived0* to return, which is why others have 
told you the casts are UB.  If you are iterating through them, you could 
add a (template) method to perform the iteration with the appropriate 
cast of the vector elements (not the vector) to your container class.

-- 
Ian Collins

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