Re: Reinterpret cast and CRTP
On 13 Jan., 14:35, Ulrich Eckhardt <eckha...@satorlaser.com> wrote:
peter koch larsen wrote:
On 12 Jan., 05:33, pfultz2 <pful...@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! ]