Commit 9771644a by Paolo Carlini Committed by Paolo Carlini

re PR libstdc++/53080 (tuple interface to std::array doesn't check bounds)

2012-04-23  Paolo Carlini  <paolo.carlini@oracle.com>

	PR libstdc++/53080
	* include/std/array (tuple_element, get): static_assert I < N.
	* testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc:
	New.
	* testsuite/23_containers/array/tuple_interface/get_neg.cc: Likewise.
	* testsuite/23_containers/array/tuple_interface/tuple_element.cc: Fix.

From-SVN: r186702
parent ff5be0e3
2012-04-23 Paolo Carlini <paolo.carlini@oracle.com>
PR libstdc++/53080
* include/std/array (tuple_element, get): static_assert I < N.
* testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc:
New.
* testsuite/23_containers/array/tuple_interface/get_neg.cc: Likewise.
* testsuite/23_containers/array/tuple_interface/tuple_element.cc: Fix.
2012-04-23 Chris Jefferson <chris@bubblescope.net> 2012-04-23 Chris Jefferson <chris@bubblescope.net>
PR testsuite/53046 PR testsuite/53046
......
// <array> -*- C++ -*- // <array> -*- C++ -*-
// Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
// Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the // software; you can redistribute it and/or modify it under the
...@@ -261,23 +262,35 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -261,23 +262,35 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
class tuple_element; class tuple_element;
template<std::size_t _Int, typename _Tp, std::size_t _Nm> template<std::size_t _Int, typename _Tp, std::size_t _Nm>
struct tuple_element<_Int, array<_Tp, _Nm> > struct tuple_element<_Int, array<_Tp, _Nm>>
{ typedef _Tp type; }; {
static_assert(_Int < _Nm, "index is out of bounds");
typedef _Tp type;
};
template<std::size_t _Int, typename _Tp, std::size_t _Nm> template<std::size_t _Int, typename _Tp, std::size_t _Nm>
constexpr _Tp& constexpr _Tp&
get(array<_Tp, _Nm>& __arr) noexcept get(array<_Tp, _Nm>& __arr) noexcept
{ return __arr._M_instance[_Int]; } {
static_assert(_Int < _Nm, "index is out of bounds");
return __arr._M_instance[_Int];
}
template<std::size_t _Int, typename _Tp, std::size_t _Nm> template<std::size_t _Int, typename _Tp, std::size_t _Nm>
constexpr _Tp&& constexpr _Tp&&
get(array<_Tp, _Nm>&& __arr) noexcept get(array<_Tp, _Nm>&& __arr) noexcept
{ return std::move(get<_Int>(__arr)); } {
static_assert(_Int < _Nm, "index is out of bounds");
return std::move(get<_Int>(__arr));
}
template<std::size_t _Int, typename _Tp, std::size_t _Nm> template<std::size_t _Int, typename _Tp, std::size_t _Nm>
constexpr const _Tp& constexpr const _Tp&
get(const array<_Tp, _Nm>& __arr) noexcept get(const array<_Tp, _Nm>& __arr) noexcept
{ return __arr._M_instance[_Int]; } {
static_assert(_Int < _Nm, "index is out of bounds");
return __arr._M_instance[_Int];
}
_GLIBCXX_END_NAMESPACE_VERSION _GLIBCXX_END_NAMESPACE_VERSION
} // namespace } // namespace
......
// { dg-options "-std=gnu++11" }
// { dg-do compile }
// Copyright (C) 2012 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include <array>
std::array<int, 1> a{};
const std::array<int, 1> ca{};
int n1 = std::get<1>(a);
int n2 = std::get<1>(std::move(a));
int n3 = std::get<1>(ca);
// { dg-error "static assertion failed" "" { target *-*-* } 275 }
// { dg-error "static assertion failed" "" { target *-*-* } 283 }
// { dg-error "static assertion failed" "" { target *-*-* } 291 }
// { dg-options "-std=gnu++0x" } // { dg-options "-std=gnu++0x" }
// //
// Copyright (C) 2011 Free Software Foundation, Inc. // Copyright (C) 2011, 2012 Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the // software; you can redistribute it and/or modify it under the
...@@ -27,19 +27,11 @@ test01() ...@@ -27,19 +27,11 @@ test01()
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
using namespace std; using namespace std;
{ const size_t len = 3;
const size_t len = 3; typedef array<int, len> array_type;
typedef array<int, len> array_type; VERIFY( (is_same<tuple_element<0, array_type>::type, int>::value == true) );
VERIFY( (is_same<tuple_element<0, array_type>::type, int>::value == true) ); VERIFY( (is_same<tuple_element<1, array_type>::type, int>::value == true) );
VERIFY( (is_same<tuple_element<1, array_type>::type, int>::value == true) ); VERIFY( (is_same<tuple_element<2, array_type>::type, int>::value == true) );
VERIFY( (is_same<tuple_element<2, array_type>::type, int>::value == true) );
}
{
const size_t len = 0;
typedef array<int, len> array_type;
VERIFY( (is_same<tuple_element<0, array_type>::type, int>::value == true) );
}
} }
int main() int main()
......
// { dg-options "-std=gnu++11" }
// { dg-do compile }
// Copyright (C) 2012 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include <array>
typedef std::tuple_element<1, std::array<int, 1>>::type type;
// { dg-error "static assertion failed" "" { target *-*-* } 267 }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment