move.fail.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // UNSUPPORTED: c++98, c++03, c++11, c++14
  9. // <optional>
  10. // constexpr optional(const optional<T>&& rhs);
  11. // C++17 said:
  12. // If is_trivially_move_constructible_v<T> is true,
  13. // this constructor shall be a constexpr constructor.
  14. //
  15. // P0602 changed this to:
  16. // If is_trivially_move_constructible_v<T> is true, this constructor is trivial.
  17. //
  18. // which means that it can't be constexpr if T is not trivially move-constructible,
  19. // because you have to do a placement new to get the value into place.
  20. // Except in the case where it is moving from an empty optional - that could be
  21. // made to be constexpr (and libstdc++ does so).
  22. #include <optional>
  23. #include <type_traits>
  24. #include <cassert>
  25. #include "test_macros.h"
  26. struct S {
  27. constexpr S() : v_(0) {}
  28. S(int v) : v_(v) {}
  29. constexpr S(const S &rhs) : v_(rhs.v_) {} // not trivially moveable
  30. constexpr S( S &&rhs) : v_(rhs.v_) {} // not trivially moveable
  31. int v_;
  32. };
  33. constexpr bool test() // expected-error {{constexpr function never produces a constant expression}}
  34. {
  35. std::optional<S> o1{3};
  36. std::optional<S> o2 = std::move(o1);
  37. return o2.has_value(); // return -something-
  38. }
  39. int main(int, char**)
  40. {
  41. static_assert (!std::is_trivially_move_constructible_v<S>, "" );
  42. static_assert (test(), ""); // expected-error {{static_assert expression is not an integral constant expression}}
  43. return 0;
  44. }