[libcxx] r305196 - Make tuple_element static_assert in pair if the index is out of range. Also, add a message to variant_alternative<> in the similar case (it already asserted). Add tests for this
Marshall Clow via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 12 09:13:17 PDT 2017
Author: marshall
Date: Mon Jun 12 11:13:17 2017
New Revision: 305196
URL: http://llvm.org/viewvc/llvm-project?rev=305196&view=rev
Log:
Make tuple_element static_assert in pair if the index is out of range. Also, add a message to variant_alternative<> in the similar case (it already asserted). Add tests for this
Added:
libcxx/trunk/test/libcxx/utilities/utility/pairs/pairs.pair/pair.tuple_element.fail.cpp
libcxx/trunk/test/libcxx/utilities/variant/variant.variant/variant.helper/
libcxx/trunk/test/libcxx/utilities/variant/variant.variant/variant.helper/variant_alternative.fail.cpp
Modified:
libcxx/trunk/include/utility
libcxx/trunk/include/variant
Modified: libcxx/trunk/include/utility
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/utility?rev=305196&r1=305195&r2=305196&view=diff
==============================================================================
--- libcxx/trunk/include/utility (original)
+++ libcxx/trunk/include/utility Mon Jun 12 11:13:17 2017
@@ -653,6 +653,12 @@ template <class _T1, class _T2>
class _LIBCPP_TEMPLATE_VIS tuple_size<pair<_T1, _T2> >
: public integral_constant<size_t, 2> {};
+template <size_t _Ip, class _T1, class _T2>
+class _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, pair<_T1, _T2> >
+{
+ static_assert(_Ip < 2, "Index out of bounds in std::tuple_element<std::pair<T1, T2>>");
+};
+
template <class _T1, class _T2>
class _LIBCPP_TEMPLATE_VIS tuple_element<0, pair<_T1, _T2> >
{
Modified: libcxx/trunk/include/variant
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/variant?rev=305196&r1=305195&r2=305196&view=diff
==============================================================================
--- libcxx/trunk/include/variant (original)
+++ libcxx/trunk/include/variant Mon Jun 12 11:13:17 2017
@@ -278,7 +278,7 @@ struct _LIBCPP_TEMPLATE_VIS variant_alte
template <size_t _Ip, class... _Types>
struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
- static_assert(_Ip < sizeof...(_Types));
+ static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
using type = __type_pack_element<_Ip, _Types...>;
};
Added: libcxx/trunk/test/libcxx/utilities/utility/pairs/pairs.pair/pair.tuple_element.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/utilities/utility/pairs/pairs.pair/pair.tuple_element.fail.cpp?rev=305196&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/utilities/utility/pairs/pairs.pair/pair.tuple_element.fail.cpp (added)
+++ libcxx/trunk/test/libcxx/utilities/utility/pairs/pairs.pair/pair.tuple_element.fail.cpp Mon Jun 12 11:13:17 2017
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <utility>
+
+// template <class T1, class T2> struct pair
+
+// tuple_element<I, pair<T1, T2> >::type
+
+#include <utility>
+
+int main()
+{
+ {
+ typedef std::pair<int, double> P;
+ std::tuple_element<2, P>::type foo; // expected-note {{requested here}}
+ // expected-error at utility:* {{static_assert failed "Index out of bounds in std::tuple_element<std::pair<T1, T2>>"}}
+ }
+}
Added: libcxx/trunk/test/libcxx/utilities/variant/variant.variant/variant.helper/variant_alternative.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/utilities/variant/variant.variant/variant.helper/variant_alternative.fail.cpp?rev=305196&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/utilities/variant/variant.variant/variant.helper/variant_alternative.fail.cpp (added)
+++ libcxx/trunk/test/libcxx/utilities/variant/variant.variant/variant.helper/variant_alternative.fail.cpp Mon Jun 12 11:13:17 2017
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <array>
+
+// template <size_t I, class T> struct variant_alternative; // undefined
+// template <size_t I, class T> struct variant_alternative<I, const T>;
+// template <size_t I, class T> struct variant_alternative<I, volatile T>;
+// template <size_t I, class T> struct variant_alternative<I, const volatile T>;
+// template <size_t I, class T>
+// using variant_alternative_t = typename variant_alternative<I, T>::type;
+//
+// template <size_t I, class... Types>
+// struct variant_alternative<I, variant<Types...>>;
+
+
+#include <variant>
+#include <cassert>
+
+
+int main()
+{
+ {
+ typedef std::variant<int, double> T;
+ std::variant_alternative<2, T>::type foo; // expected-note {{requested here}}
+ // expected-error at variant:* {{static_assert failed "Index out of bounds in std::variant_alternative<>"}}
+ }
+}
More information about the cfe-commits
mailing list