Add constexpr support to Boost.Array

[SVN r82834]
This commit is contained in:
Marshall Clow
2013-02-12 18:07:15 +00:00
parent 117584a2ce
commit 4a60b8c146
3 changed files with 64 additions and 24 deletions

View File

@ -13,6 +13,7 @@
* accompanying file LICENSE_1_0.txt or copy at * accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt) * http://www.boost.org/LICENSE_1_0.txt)
* *
* 9 Jan 2013 - (mtc) Added constexpr
* 14 Apr 2012 - (mtc) Added support for boost::hash * 14 Apr 2012 - (mtc) Added support for boost::hash
* 28 Dec 2010 - (mtc) Added cbegin and cend (and crbegin and crend) for C++Ox compatibility. * 28 Dec 2010 - (mtc) Added cbegin and cend (and crbegin and crend) for C++Ox compatibility.
* 10 Mar 2010 - (mtc) fill method added, matching resolution of the standard library working group. * 10 Mar 2010 - (mtc) fill method added, matching resolution of the standard library working group.
@ -121,19 +122,17 @@ namespace boost {
// operator[] // operator[]
reference operator[](size_type i) reference operator[](size_type i)
{ {
BOOST_ASSERT_MSG( i < N, "out of range" ); return BOOST_ASSERT_MSG( i < N, "out of range" ), elems[i];
return elems[i];
} }
const_reference operator[](size_type i) const BOOST_CONSTEXPR const_reference operator[](size_type i) const
{ {
BOOST_ASSERT_MSG( i < N, "out of range" ); return BOOST_ASSERT_MSG( i < N, "out of range" ), elems[i];
return elems[i];
} }
// at() with range check // at() with range check
reference at(size_type i) { rangecheck(i); return elems[i]; } reference at(size_type i) { return rangecheck(i), elems[i]; }
const_reference at(size_type i) const { rangecheck(i); return elems[i]; } BOOST_CONSTEXPR const_reference at(size_type i) const { return rangecheck(i), elems[i]; }
// front() and back() // front() and back()
reference front() reference front()
@ -141,7 +140,7 @@ namespace boost {
return elems[0]; return elems[0];
} }
const_reference front() const BOOST_CONSTEXPR const_reference front() const
{ {
return elems[0]; return elems[0];
} }
@ -151,15 +150,15 @@ namespace boost {
return elems[N-1]; return elems[N-1];
} }
const_reference back() const BOOST_CONSTEXPR const_reference back() const
{ {
return elems[N-1]; return elems[N-1];
} }
// size is constant // size is constant
static size_type size() { return N; } static BOOST_CONSTEXPR size_type size() { return N; }
static bool empty() { return false; } static BOOST_CONSTEXPR bool empty() { return false; }
static size_type max_size() { return N; } static BOOST_CONSTEXPR size_type max_size() { return N; }
enum { static_size = N }; enum { static_size = N };
// swap (note: linear complexity) // swap (note: linear complexity)
@ -190,11 +189,8 @@ namespace boost {
} }
// check range (may be private because it is static) // check range (may be private because it is static)
static void rangecheck (size_type i) { static BOOST_CONSTEXPR bool rangecheck (size_type i) {
if (i >= size()) { return i > size() ? boost::throw_exception(std::out_of_range ("array<>: index out of range")), true : true;
std::out_of_range e("array<>: index out of range");
boost::throw_exception(e);
}
} }
}; };
@ -265,14 +261,14 @@ namespace boost {
return failed_rangecheck(); return failed_rangecheck();
} }
const_reference operator[](size_type /*i*/) const BOOST_CONSTEXPR const_reference operator[](size_type /*i*/) const
{ {
return failed_rangecheck(); return failed_rangecheck();
} }
// at() with range check // at() with range check
reference at(size_type /*i*/) { return failed_rangecheck(); } reference at(size_type /*i*/) { return failed_rangecheck(); }
const_reference at(size_type /*i*/) const { return failed_rangecheck(); } BOOST_CONSTEXPR const_reference at(size_type /*i*/) const { return failed_rangecheck(); }
// front() and back() // front() and back()
reference front() reference front()
@ -280,7 +276,7 @@ namespace boost {
return failed_rangecheck(); return failed_rangecheck();
} }
const_reference front() const BOOST_CONSTEXPR const_reference front() const
{ {
return failed_rangecheck(); return failed_rangecheck();
} }
@ -290,15 +286,15 @@ namespace boost {
return failed_rangecheck(); return failed_rangecheck();
} }
const_reference back() const BOOST_CONSTEXPR const_reference back() const
{ {
return failed_rangecheck(); return failed_rangecheck();
} }
// size is constant // size is constant
static size_type size() { return 0; } static BOOST_CONSTEXPR size_type size() { return 0; }
static bool empty() { return true; } static BOOST_CONSTEXPR bool empty() { return true; }
static size_type max_size() { return 0; } static BOOST_CONSTEXPR size_type max_size() { return 0; }
enum { static_size = 0 }; enum { static_size = 0 };
void swap (array<T,0>& /*y*/) { void swap (array<T,0>& /*y*/) {

View File

@ -18,6 +18,7 @@ test-suite array :
[ run array5.cpp ] [ run array5.cpp ]
[ run array6.cpp unit_test_framework : : : : array6 ] [ run array6.cpp unit_test_framework : : : : array6 ]
[ run array7.cpp unit_test_framework : : : : array7 ] [ run array7.cpp unit_test_framework : : : : array7 ]
[ run array_constexpr.cpp unit_test_framework : : : : array_constexpr ]
[ compile-fail array_getfail1.cpp ] [ compile-fail array_getfail1.cpp ]
[ compile-fail array_getfail2.cpp ] [ compile-fail array_getfail2.cpp ]
[ run array_hash.cpp unit_test_framework : : : : array_hash ] [ run array_hash.cpp unit_test_framework : : : : array_hash ]

43
test/array_constexpr.cpp Normal file
View File

@ -0,0 +1,43 @@
/* tests using constexpr on boost:array
* (C) Copyright Marshall Clow 2012
* Distributed under the Boost Software License, Version 1.0. (See
* accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
#include <string>
#include <iostream>
#include <boost/array.hpp>
#include <algorithm>
#ifndef BOOST_NO_CXX11_HDR_ARRAY
#include <array>
#endif
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#ifndef BOOST_NO_CXX11_CONSTEXPR
constexpr boost::array<int, 10> arr {{ 0,1,2,3,4,5,6,7,8,9 }};
constexpr std::array<int, 10> arr_std {{ 0,1,2,3,4,5,6,7,8,9 }};
template <typename T>
void sink ( T t ) {}
template <typename T, size_t N>
void sink ( boost::array<T,N> &arr ) {}
BOOST_AUTO_TEST_CASE( test_main )
{
// constexpr int two = arr_std.at (2);
constexpr int three = arr.at (3);
int whatever [ arr.at(4) ];
(void)three;
(void) whatever;
}
#else // no constexpr means no constexpr tests!
BOOST_AUTO_TEST_CASE( test_main )
{
}
#endif