0

Suppose I have the following:

struct f
{
    static int foo()
    {
        return 42;
    }
};


int main()
{
    f* x = 0;
    return x->foo();
}

Is it UB to do this? I would argue that this is legal because I am not actually dereferencing the null pointer, only using its type info to call a static member function.....

DarthRubik
  • 3,927
  • 1
  • 18
  • 54
  • 1
    @DeiDei I know ha, I changed it before the England game with Sweeden and there's apparently a 30 day waiting time before I can change it again :( – George Jul 14 '18 at 01:36
  • Unfortunately the accepted answer is wrong – Lightness Races in Orbit Jul 14 '18 at 02:34
  • @LightnessRacesinOrbit: I think so too; do you agree that https://stackoverflow.com/a/28483477/4323 is more correct? If so, what can be done to rectify this? – John Zwinck Jul 14 '18 at 03:02
  • @JohnZwinck Yes and I don't know and I'm staying out of it because to be honest I'm equivocating a bit - your linked answer is very pedantic but gives us a way out, in this situation where the committee is almost universally against us. Personally I can't see how "dereferences the pointer to the object" can be considered well-defined when there isn't an object, and I'm not buying any of the "but I didn't convert the expression to an rvalue" rationales. I see no reason for `p = 0; *p;` to ever be valid. But, again, I am not in the majority there sadly :( – Lightness Races in Orbit Jul 19 '18 at 16:21

0 Answers0