C++ Logo

SG12

Advanced search

Subject: [ub] Is dereferencing this pointer a UB?
From: Andrzej Krzemienski (akrzemi1_at_[hidden])
Date: 2017-08-11 02:58:22


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;



SG12 list run by herb.sutter at gmail.com