[cfe-commits] [libcxx] r170435 - in /libcxx/trunk: include/array test/containers/sequences/array/array.tuple/get.fail.cpp
Marshall Clow
mclow at qualcomm.com
Tue Dec 18 08:46:30 PST 2012
Author: marshall
Date: Tue Dec 18 10:46:30 2012
New Revision: 170435
URL: http://llvm.org/viewvc/llvm-project?rev=170435&view=rev
Log:
Added static_assert to std::get<N>(std::array) calls to catch "out of bounds" calls
Added:
libcxx/trunk/test/containers/sequences/array/array.tuple/get.fail.cpp
Modified:
libcxx/trunk/include/array
Modified: libcxx/trunk/include/array
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/array?rev=170435&r1=170434&r2=170435&view=diff
==============================================================================
--- libcxx/trunk/include/array (original)
+++ libcxx/trunk/include/array Tue Dec 18 10:46:30 2012
@@ -310,6 +310,7 @@
_Tp&
get(array<_Tp, _Size>& __a) _NOEXCEPT
{
+ static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)");
return __a[_Ip];
}
@@ -318,6 +319,7 @@
const _Tp&
get(const array<_Tp, _Size>& __a) _NOEXCEPT
{
+ static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)");
return __a[_Ip];
}
@@ -328,6 +330,7 @@
_Tp&&
get(array<_Tp, _Size>&& __a) _NOEXCEPT
{
+ static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)");
return _VSTD::move(__a[_Ip]);
}
Added: libcxx/trunk/test/containers/sequences/array/array.tuple/get.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/sequences/array/array.tuple/get.fail.cpp?rev=170435&view=auto
==============================================================================
--- libcxx/trunk/test/containers/sequences/array/array.tuple/get.fail.cpp (added)
+++ libcxx/trunk/test/containers/sequences/array/array.tuple/get.fail.cpp Tue Dec 18 10:46:30 2012
@@ -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.
+//
+//===----------------------------------------------------------------------===//
+
+// <array>
+
+// template <size_t I, class T, size_t N> T& get(array<T, N>& a);
+
+#include <array>
+#include <cassert>
+
+int main()
+{
+ {
+ typedef double T;
+ typedef std::array<T, 3> C;
+ C c = {1, 2, 3.5};
+ std::get<3>(c) = 5.5; // Can't get element 3!
+ }
+}
More information about the cfe-commits
mailing list