commit 69c72d9bb802fd5e4f2704f0fe8a041f8b26d8bd
Author: Tim Shen <timshen@google.com>
Date: Mon Nov 21 21:29:13 2016 -0800
2016-11-22 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.
@@ -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
@@ -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>, "");
+}