Date: Thu, 11 Jul 2024 18:14:57 +0200
Of course it segfaults, as Base does not have buffer.
Try this:
#include <new>
struct Base { virtual ~Base(void) = default; };
struct Derived : Base {
char buf[1024u];
void Wipe(void) { ::new(buf) char[1024u]{}; }
};
void Bad_Function(Base *const p)
{
Derived *pd = static_cast<Derived*>(p);
pd->Wipe();
}
int main(void)
{
Derived var; // Var is of type Derived and have buf to work with
Base* bptr = &var; // And bptr is Base* pointing to Derived var
Bad_Function(bptr); // Now static_cast does as expected
}
On Thu, Jul 11, 2024 at 9:34 AM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
> On Wed, Jul 10, 2024 at 15:33 PM Thiago wrote:
> >>
> >> if ( typeid(*p) == typeid(MostDerived) ) pd = p + offset;
> >> else if ( typeid(*p) == typeid(MostDerived2) ) pd = p + offset2;
> >> else if ( typeid(*p) == typeid(MostDerived3) ) pd = p + offset3;
> >> else pd = dynamic_cast<Derived*>(p);
> >
> > You can just use static_cast there.
>
>
> I wasn't sure at first so I wrote the following small program:
>
> #include <new>
>
> struct Base { virtual ~Base(void) = default; };
>
> struct Derived : Base {
> char buf[1024u];
> void Wipe(void) { ::new(buf) char[1024u]{}; }
> };
>
> void Bad_Function(Base *const p)
> {
> Derived *pd = static_cast<Derived*>(p);
> pd->Wipe();
> }
>
> int main(void)
> {
> Base var;
> Bad_Function(&var);
> }
>
> This program successfully compiles, and as expected, segfaults. I've
> never seen 'static_cast' used to perform a downcast like that, and I
> didn't realise it had this functionality. I wonder who thought that
> this would be a good feature? 'static_cast' should not be able to do
> stuff like this. 'static_cast' should be a much weaker cast than this,
> to do simple things such as making an implicit conversion explicit...
> it shouldn't be able to cast to a different type that results in the
> calling of a method that segfaults.
>
> Would anyone agree with me that this functionality should be taken
> away from 'static_cast'? i.e. deprecated in C++26 and removed in
> C++29. It could be replaced with a standard library function
> "std::explicit_down_cast".
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
Try this:
#include <new>
struct Base { virtual ~Base(void) = default; };
struct Derived : Base {
char buf[1024u];
void Wipe(void) { ::new(buf) char[1024u]{}; }
};
void Bad_Function(Base *const p)
{
Derived *pd = static_cast<Derived*>(p);
pd->Wipe();
}
int main(void)
{
Derived var; // Var is of type Derived and have buf to work with
Base* bptr = &var; // And bptr is Base* pointing to Derived var
Bad_Function(bptr); // Now static_cast does as expected
}
On Thu, Jul 11, 2024 at 9:34 AM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
> On Wed, Jul 10, 2024 at 15:33 PM Thiago wrote:
> >>
> >> if ( typeid(*p) == typeid(MostDerived) ) pd = p + offset;
> >> else if ( typeid(*p) == typeid(MostDerived2) ) pd = p + offset2;
> >> else if ( typeid(*p) == typeid(MostDerived3) ) pd = p + offset3;
> >> else pd = dynamic_cast<Derived*>(p);
> >
> > You can just use static_cast there.
>
>
> I wasn't sure at first so I wrote the following small program:
>
> #include <new>
>
> struct Base { virtual ~Base(void) = default; };
>
> struct Derived : Base {
> char buf[1024u];
> void Wipe(void) { ::new(buf) char[1024u]{}; }
> };
>
> void Bad_Function(Base *const p)
> {
> Derived *pd = static_cast<Derived*>(p);
> pd->Wipe();
> }
>
> int main(void)
> {
> Base var;
> Bad_Function(&var);
> }
>
> This program successfully compiles, and as expected, segfaults. I've
> never seen 'static_cast' used to perform a downcast like that, and I
> didn't realise it had this functionality. I wonder who thought that
> this would be a good feature? 'static_cast' should not be able to do
> stuff like this. 'static_cast' should be a much weaker cast than this,
> to do simple things such as making an implicit conversion explicit...
> it shouldn't be able to cast to a different type that results in the
> calling of a method that segfaults.
>
> Would anyone agree with me that this functionality should be taken
> away from 'static_cast'? i.e. deprecated in C++26 and removed in
> C++29. It could be replaced with a standard library function
> "std::explicit_down_cast".
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
Received on 2024-07-11 16:15:11