From 97d79073cbc7c44f067bf1f2b265493ceedf9e29 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Fri, 24 Nov 2023 15:43:55 +0000 Subject: [PATCH] New issue: LWG 3973 broke const overloads of std::optional monadic operations --- xml/issue4015.xml | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 xml/issue4015.xml diff --git a/xml/issue4015.xml b/xml/issue4015.xml new file mode 100644 index 0000000000..508f00c1d8 --- /dev/null +++ b/xml/issue4015.xml @@ -0,0 +1,68 @@ + + + + +LWG 3973 broke <code>const</code> overloads of <code>std::optional</code> monadic operations +
+Jonathan Wakely +24 Nov 2023 +99 + + +

+The resolution of LWG (adopted in Kona) changed all +occurrences of value() to *val. +The intention was not to change the meaning, just avoid the non-freestanding +value() function, and avoid ADL that would be caused by using +**this. +However, in the const overloads such as +and_then(F&&) const the type of value() +was const T&, but the type of *val is always +T&. This implies that the const overloads invoke the callable +with a non-const argument, which is incorrect (and would be undefined +behaviour for a const std::optional<T>). + +

+

+On the LWG reflector it was suggested that we should rewrite the specification +of std::optional to stop using an exposition-only data member +of type T*. No such member ever exists in real implemetations, +so it is misleading and leads to specification bugs of this sort. +

+

+Change the class definition in +to use a union, and update every use of val accordingly +throughout . +For consistency with we might +also want to introduce a bool has_val member and refer to +that in the specification. +

+
+

+  private:
+    T *val;         // exposition only
+    bool has_val;   // exposition only
+    union {
+      T val;        // exposition only
+    };
+  };
+
+
+

For example, in :

+
+

+-1- Effects: +If *this contains a value, calls +val->.T::~T() to destroy the contained +value and sets has_val to false; +otherwise no effect. +

+
+
+ + +

+

+
+ +