]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Add constexpr to std::optional::value_or(U&&)&&
authorredi <redi@138bc75d-0d04-0410-961f-82ee72b054a4>
Wed, 17 Apr 2019 13:13:30 +0000 (13:13 +0000)
committerredi <redi@138bc75d-0d04-0410-961f-82ee72b054a4>
Wed, 17 Apr 2019 13:13:30 +0000 (13:13 +0000)
In C++1z drafts up to N4606 the constexpr keyword was missing from the
detailed description of this function, despite being shown in the class
synopsis.  That was fixed editorially for N4618, but our implementation
was not corrected to match.

* include/std/optional (optional::value_or(U&&) &&): Add missing
constexpr specifier.
* testsuite/20_util/optional/constexpr/observers/4.cc: Check value_or
for disengaged optionals and rvalue optionals.
* testsuite/20_util/optional/observers/4.cc: Likewise.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@270409 138bc75d-0d04-0410-961f-82ee72b054a4

libstdc++-v3/ChangeLog
libstdc++-v3/include/std/optional
libstdc++-v3/testsuite/20_util/optional/constexpr/observers/4.cc
libstdc++-v3/testsuite/20_util/optional/observers/4.cc

index 55d2538b76e869eb841b9baaf65fb88cf273e5bf..9ab8bb9154a5dc7cca236a907b75fbd8e7baaf10 100644 (file)
@@ -1,3 +1,11 @@
+2019-04-17  Jonathan Wakely  <jwakely@redhat.com>
+
+       * include/std/optional (optional::value_or(U&&) &&): Add missing
+       constexpr specifier.
+       * testsuite/20_util/optional/constexpr/observers/4.cc: Check value_or
+       for disengaged optionals and rvalue optionals.
+       * testsuite/20_util/optional/observers/4.cc: Likewise.
+
 2019-04-12  Thomas Rodgers  <trodgers@redhat.com>
        
        * include/pstl/algorithm_impl.h: Uglify identfiers.
index d243930fed4906285a68b5c640e75f00da57ff07..503d859bee6fcabba079951c7a12e2a2cd0dc12a 100644 (file)
@@ -959,7 +959,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        }
 
       template<typename _Up>
-       _Tp
+       constexpr _Tp
        value_or(_Up&& __u) &&
        {
          static_assert(is_move_constructible_v<_Tp>);
index 1f7f0e8b6a282822b8e88a54037cef255bd899a2..a085f53f8fab6be4f8c9bd6185bfb541f0a968d0 100644 (file)
@@ -25,10 +25,42 @@ struct value_type
   int i;
 };
 
-int main()
+void test01()
 {
   constexpr std::optional<value_type> o { value_type { 51 } };
   constexpr value_type fallback { 3 };
-  static_assert( o.value_or(fallback).i == 51, "" );
-  static_assert( o.value_or(fallback).i == (*o).i, "" );
+  static_assert( o.value_or(fallback).i == 51 );
+  static_assert( o.value_or(fallback).i == (*o).i );
+}
+
+void test02()
+{
+  constexpr std::optional<value_type> o;
+  constexpr value_type fallback { 3 };
+  static_assert( o.value_or(fallback).i == 3 );
+}
+
+template<typename T>
+  constexpr std::optional<value_type>
+  make_rvalue(T t)
+  { return std::optional<value_type>{t}; }
+
+void test03()
+{
+  constexpr value_type fallback { 3 };
+  static_assert( make_rvalue(value_type{51}).value_or(fallback).i == 51 );
+}
+
+void test04()
+{
+  constexpr value_type fallback { 3 };
+  static_assert( make_rvalue(std::nullopt).value_or(fallback).i == 3 );
+}
+
+int main()
+{
+  test01();
+  test02();
+  test03();
+  test04();
 }
index c24e4e6856ec89b2bb800d5628c59fd08a252863..5d608cdeaf7331a03212e712647c025ee2d8fc7c 100644 (file)
@@ -26,10 +26,42 @@ struct value_type
   int i;
 };
 
-int main()
+void test01()
 {
   std::optional<value_type> o { value_type { 51 } };
   value_type fallback { 3 };
   VERIFY( o.value_or(fallback).i == 51 );
   VERIFY( o.value_or(fallback).i == (*o).i );
 }
+
+void test02()
+{
+  std::optional<value_type> o;
+  value_type fallback { 3 };
+  VERIFY( o.value_or(fallback).i == 3 );
+}
+
+void test03()
+{
+  std::optional<value_type> o { value_type { 51 } };
+  value_type fallback { 3 };
+  VERIFY( std::move(o).value_or(fallback).i == 51 );
+  VERIFY( o.has_value() );
+  VERIFY( std::move(o).value_or(fallback).i == (*o).i );
+}
+
+void test04()
+{
+  std::optional<value_type> o;
+  value_type fallback { 3 };
+  VERIFY( std::move(o).value_or(fallback).i == 3 );
+  VERIFY( !o.has_value() );
+}
+
+int main()
+{
+  test01();
+  test02();
+  test03();
+  test04();
+}