3

Possible Duplicate:
When does invoking a member function on a null instance result in undefined behavior?

I just read this question with an excellent answer: When does invoking a member function on a null instance result in undefined behavior?

Basically, is the following code undefined behaviour?

struct foo { static void bar() { } };
foo *p = nullptr;
p->bar();

According to the linked post, this can be interpreted in different ways one being UB and one being not.

In C++0x, as of n3126, the ambiguity remains

Does this still hold with final C++11?

Community
  • 1
  • 1
helami
  • 2,099
  • 2
  • 14
  • 17
  • 6
    Quoting the answer in the linked question: "It's always undefined behavior to call a member function through a null pointer". – mfontanini Jan 23 '13 at 21:44
  • Just because it may be allowed doesn't mean you should do it. – Pubby Jan 23 '13 at 21:46
  • Quoting the committee in [this defect](http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_closed.html#315): `*p is not an error when p is null unless the lvalue is converted to an rvalue`. There is no reason why this should not be legal. – helami Jan 23 '13 at 21:47
  • @helami, Note that it mentions static member functions, not instanced ones. – chris Jan 23 '13 at 21:48
  • 3
    @helami you misread the post you linked. – Yakk - Adam Nevraumont Jan 23 '13 at 21:49
  • 1
    @Yakk: How so? He wants to know whether those answers are still true in C++11. – Lightness Races in Orbit Jan 23 '13 at 21:51
  • @Non-StopTimeTravel: Note the distinction made between static and non-static member functions. The ambiguity is talking about *static* member functions. The OP misread the post. – Jesse Good Jan 23 '13 at 21:55
  • @JesseGood: Okay; once he corrects his code example, it'll be a valid question. – Lightness Races in Orbit Jan 23 '13 at 21:56
  • Ok, I agree with you that the example was wrong. I've edited my question. – helami Jan 23 '13 at 21:57
  • 1
    @Non-StopTimeTravel: That is already answered in [Jame's McNellis comment](http://stackoverflow.com/questions/2474018/when-does-invoking-a-member-function-on-a-null-instance-result-in-undefined-beha#comment3058181_2474021). – Jesse Good Jan 23 '13 at 21:59
  • I think this would work better as a comment to my original answer than a new exceedingly-duplicate-like question. I'll happily re-research my answer to the latest draft, but AFAIK the resolution is still the same: ambiguous. – GManNickG Jan 23 '13 at 22:54
  • @JesseGood: No, it is not, since the comment was written more than a year before C++11 existed. This question is about _whether such logic still applies in C++11_. It is time sensitive. – Lightness Races in Orbit Jan 24 '13 at 00:30

1 Answers1

5

The question you linked clearly shows that in either the strict or weak interpretation of the standard, the code you show is undefined behavior. The ambiguity (may) only exist for static functions (and your question refers specifically to non-static functions).

EDIT: The ambiguity yet remains in N3337 dated 2012-01-16 but I don't have a copy of the final standard. Based on the comments in the issue, it looks like the resolution to issue 232 never made it into the standard, apparently because the wording was too strong regarding making it a compile-time concept rather than undefined behavior as intended.

Mark B
  • 95,107
  • 10
  • 109
  • 188