C++ Logo

std-discussion

Advanced search

Indirection of nullptr that is not really done

From: Yongwei Wu <wuyongwei_at_[hidden]>
Date: Sat, 22 Jun 2024 14:14:16 +0800
I saw some discussions about whether some code is undefined behavior. Example:

class Obj {
public:
  int f() { return d_; }
  void g() {}
  static void h() {}

private:
  int d_;
};

auto ptr = static_cast<Obj*>(nullptr);
ptr->f(); // Clearly undefined
ptr->g(); // Arguably undefined
ptr->h(); // ?

In the last two cases, nullptr is never dereferenced, especially the
ptr->h() case, where the object pointer is not used at all. Is the
last case really undefined?

https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_closed.html#315

According to this issue, ptr->h() is OK.

https://cplusplus.github.io/CWG/issues/2823.html

According to this issue, ptr->h() is undefined.

Should we mention lvalue-to-rvalue conversion in expr.unary.op/1?

Thoughts?

-- 
Yongwei Wu
URL: http://wyw.dcweb.cn/

Received on 2024-06-22 06:14:30