Prev: reasoning of ::std::cout (ostream) conversion w.r.t. plain, signed and unsigned chars...
Next: ANNOUNCE: just::thread library V1.3 released
From: peter koch larsen on 12 Jan 2010 08:58 On 12 Jan., 05:33, pfultz2 <pful...(a)yahoo.com> wrote: > When i use the curiously recurring template pattern, do i need to use > the reinterpret_cast to get the derived class or do i use the > static_cast? And is there a performance penalty in doing this cast? > For example > template<class Derived> > class Base > { > public: > void foo() > { > reinterpret_cast<Derived*>(this)->bar(); //or can i use a static > cast here? > > } > }; > > class MyClass : Base<MyClass> > { > public: > void bar();}; > > Im under the assumption that a static cast wouldnt work since the > derived class is not known at compile time. And if it werent then > would a reinterpret_cast be safe, if it were always used this way?? The best would be to use a dynamic_cast. If this is not available to you, you should use a static_cast. reinterpret_cast should never be used in situations like this and might well give a wrong result. /Peter -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Seungbeom Kim on 12 Jan 2010 19:37 peter koch larsen wrote: > On 12 Jan., 05:33, pfultz2 <pful...(a)yahoo.com> wrote: >> When i use the curiously recurring template pattern, do i need to use >> the reinterpret_cast to get the derived class or do i use the >> static_cast? And is there a performance penalty in doing this cast? [...] >> reinterpret_cast<Derived*>(this)->bar(); //or can i use a static >> cast here? [...] > > The best would be to use a dynamic_cast. If this is not available to > you, you should use a static_cast. reinterpret_cast should never be > used in situations like this and might well give a wrong result. No, dynamic_cast works is not only necessary (though it might give you additional peace of mind), but doesn't work in all cases -- it doesn't work unless the class is polymorphic (i.e. having at least one virtual member function). -- Seungbeom Kim [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ulrich Eckhardt on 12 Jan 2010 20:35 peter koch larsen wrote: > On 12 Jan., 05:33, pfultz2 <pful...(a)yahoo.com> wrote: >> When i use the curiously recurring template pattern, do i need to use >> the reinterpret_cast to get the derived class or do i use the >> static_cast? [...] >> Im under the assumption that a static cast wouldnt work since the >> derived class is not known at compile time. And if it werent then >> would a reinterpret_cast be safe, if it were always used this way?? > > The best would be to use a dynamic_cast. If this is not available to > you, you should use a static_cast. In the given example it is not available because MyClass is not polymorphic, it doesn't have any virtual functions, hence no RTTI and no dynamic cast. That said, I would actually accept dynamic_cast for checking in diagnostic mode, but that's all. Uli -- Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ulrich Eckhardt on 13 Jan 2010 06:00 Ulrich Eckhardt wrote: [MyClass is derived from a class using CRTP] > In the given example it is not available because MyClass is not > polymorphic [...] Actually, this statement is wrong. The source type, in this case the CRTP class, must already be polymorphic and not just the target type. The target type, which is derived from the source, is then also polymorphic of course. Uli -- Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: peter koch larsen on 16 Jan 2010 20:18
On 13 Jan., 14:35, Ulrich Eckhardt <eckha...(a)satorlaser.com> wrote: > peter koch larsen wrote: > > On 12 Jan., 05:33, pfultz2 <pful...(a)yahoo.com> wrote: > >> When i use the curiously recurring template pattern, do i need to use > >> the reinterpret_cast to get the derived class or do i use the > >> static_cast? > [...] > >> Im under the assumption that a static cast wouldnt work since the > >> derived class is not known at compile time. And if it werent then > >> would a reinterpret_cast be safe, if it were always used this way?? > > > The best would be to use a dynamic_cast. If this is not available to > > you, you should use a static_cast. > > In the given example it is not available because MyClass is not polymorphic, > it doesn't have any virtual functions, hence no RTTI and no dynamic cast. > That said, I would actually accept dynamic_cast for checking in diagnostic > mode, but that's all. That is correct and what I meant with a dynamic_cast being available. Apart from that, I overlooked that the cast happened in a "safe" place, assuming that Base is always a base of Derived. What I don't understand is your comment to your own post. There is no polymorphism and in the actual case dynamic_cast will not be available. /Peter -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |