Update test to better check for the non-constexpr-ness of a move constructor. Fixes...
authorMarshall Clow <mclow.lists@gmail.com>
Thu, 25 Apr 2019 02:12:51 +0000 (02:12 +0000)
committerMarshall Clow <mclow.lists@gmail.com>
Thu, 25 Apr 2019 02:12:51 +0000 (02:12 +0000)
llvm-svn: 359162

libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/move.fail.cpp

index a8634b9..8aae557 100644 (file)
 // <optional>
 
 // constexpr optional(const optional<T>&& rhs);
+//  C++17 said:
 //   If is_trivially_move_constructible_v<T> is true,
 //    this constructor shall be a constexpr constructor.
+//
+//  P0602 changed this to:
+//     If is_trivially_move_constructible_v<T> is true, this constructor is trivial.
+//
+//  which means that it can't be constexpr if T is not trivially move-constructible,
+//    because you have to do a placement new to get the value into place.
+//    Except in the case where it is moving from an empty optional - that could be
+//    made to be constexpr (and libstdc++ does so).
 
 #include <optional>
 #include <type_traits>
@@ -23,16 +32,22 @@ struct S {
     constexpr S()   : v_(0) {}
     S(int v)        : v_(v) {}
     constexpr S(const S  &rhs) : v_(rhs.v_) {} // not trivially moveable
-    constexpr S(const S &&rhs) : v_(rhs.v_) {} // not trivially moveable
+    constexpr S(      S &&rhs) : v_(rhs.v_) {} // not trivially moveable
     int v_;
     };
 
 
+constexpr bool test() // expected-error {{constexpr function never produces a constant expression}}
+{
+    std::optional<S> o1{3};
+    std::optional<S> o2 = std::move(o1);
+    return o2.has_value();  // return -something-
+}
+
+
 int main(int, char**)
 {
     static_assert (!std::is_trivially_move_constructible_v<S>, "" );
-    constexpr std::optional<S> o1;
-    constexpr std::optional<S> o2 = std::move(o1);  // not constexpr
-
-  return 0;
+    static_assert (test(), "");  // expected-error {{static_assert expression is not an integral constant expression}}
+    return 0;
 }