re PR libstdc++/78441 ([variant] variant_alternative doesn't allow cv qualifiers)
authorTim Shen <timshen@google.com>
Sun, 27 Nov 2016 00:32:04 +0000 (00:32 +0000)
committerTim Shen <timshen@gcc.gnu.org>
Sun, 27 Nov 2016 00:32:04 +0000 (00:32 +0000)
PR libstdc++/78441
* include/std/variant: Propagate cv qualifications to types returned
by variant_alternative.
* testsuite/20_util/variant/compile.cc: Tests.

From-SVN: r242892

libstdc++-v3/ChangeLog
libstdc++-v3/include/std/variant
libstdc++-v3/testsuite/20_util/variant/compile.cc

index d663f70962f2967b5ae23ec39f3d98dd96094da3..4a26191fb8fe95c16dad926fc3e6a1c672a1935f 100644 (file)
@@ -1,3 +1,10 @@
+2016-11-26  Tim Shen  <timshen@google.com>
+
+       PR libstdc++/78441
+       * include/std/variant: Propagate cv qualifications to types returned
+       by variant_alternative.
+       * testsuite/20_util/variant/compile.cc: Tests.
+
 2016-11-23  Felix Morgner  <felix.morgner@gmail.com>
            Jonathan Wakely  <jwakely@redhat.com>
 
index 7d9357537289ecd9436021364014f64eadb3634e..34ad3fdefb0081e8974642be5ac1ac2e6858636d 100644 (file)
@@ -85,6 +85,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     using variant_alternative_t =
       typename variant_alternative<_Np, _Variant>::type;
 
+  template<size_t _Np, typename _Variant>
+    struct variant_alternative<_Np, const _Variant>
+    { using type = add_const_t<variant_alternative_t<_Np, _Variant>>; };
+
+  template<size_t _Np, typename _Variant>
+    struct variant_alternative<_Np, volatile _Variant>
+    { using type = add_volatile_t<variant_alternative_t<_Np, _Variant>>; };
+
+  template<size_t _Np, typename _Variant>
+    struct variant_alternative<_Np, const volatile _Variant>
+    { using type = add_cv_t<variant_alternative_t<_Np, _Variant>>; };
+
   constexpr size_t variant_npos = -1;
 
 _GLIBCXX_END_NAMESPACE_VERSION
index 2470bcc7f9eac46eab7d7811075aeb0f8b1af08c..e3330bef928cc88ea5c6d09c81dd6f5d36dff669 100644 (file)
@@ -330,3 +330,12 @@ void test_adl()
    variant<X> v8{allocator_arg, a, in_place_type<X>, il, x};
    variant<X> v9{allocator_arg, a, in_place_type<X>, 1};
 }
+
+void test_variant_alternative() {
+  static_assert(is_same_v<variant_alternative_t<0, variant<int, string>>, int>, "");
+  static_assert(is_same_v<variant_alternative_t<1, variant<int, string>>, string>, "");
+
+  static_assert(is_same_v<variant_alternative_t<0, const variant<int>>, const int>, "");
+  static_assert(is_same_v<variant_alternative_t<0, volatile variant<int>>, volatile int>, "");
+  static_assert(is_same_v<variant_alternative_t<0, const volatile variant<int>>, const volatile int>, "");
+}