[ub] Is dereferencing this pointer a UB?

Andrzej Krzemienski akrzemi1 at gmail.com
Fri Aug 11 09:58:22 CEST 2017


Hi SG12 Members,

I already asked this question in ISO C++ Standard - Discussion  (
https://groups.google.com/a/isocpp.org/forum/?fromgroups=#!topic/std-discussion/UbROFU6Fs0E),
but maybe this list is better suited.

UB-sanitizer reports a runtime error for the following program:

```
struct B;

struct I {
  virtual void f() {}; // <- virtual
};

struct A : I {
  A();
};

struct B : A {
};

A::A() { *static_cast<B*>(this); } // <- UB in static_cast

int main()
{
  B{};
}
```

My question: is UB-sanitizer correct? Is this a UB according to the
standard? And if so, could you point me to the relevant sections?

Some people point out that pointer dereference means accessing the (not yet
constructed) object, but according to my reading referring to an object is
not access. And also, UB sanitizer reports an UB when there is no
dereference:


```
struct B;

struct I {
  virtual void f() {}; // <- virtual
};

struct A : I {
  A();
};

struct B : A {
};

A::A() { static_cast<B*>(this); } // <- No dereference

int main()
{
  B{};
}
```
Is this second example a UB also?

Regards,
&rzej;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.open-std.org/pipermail/ub/attachments/20170811/f6c429eb/attachment.html 


More information about the ub mailing list