Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r82834 - in trunk: boost libs/array/test
From: marshall_at_[hidden]
Date: 2013-02-12 13:07:16


Author: marshall
Date: 2013-02-12 13:07:15 EST (Tue, 12 Feb 2013)
New Revision: 82834
URL: http://svn.boost.org/trac/boost/changeset/82834

Log:
Add constexpr support to Boost.Array
Added:
   trunk/libs/array/test/array_constexpr.cpp (contents, props changed)
Text files modified:
   trunk/boost/array.hpp | 44 ++++++++++++++++++---------------------
   trunk/libs/array/test/Jamfile.v2 | 1
   2 files changed, 21 insertions(+), 24 deletions(-)

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

Modified: trunk/libs/array/test/Jamfile.v2
==============================================================================
--- trunk/libs/array/test/Jamfile.v2 (original)
+++ trunk/libs/array/test/Jamfile.v2 2013-02-12 13:07:15 EST (Tue, 12 Feb 2013)
@@ -18,6 +18,7 @@
     [ run array5.cpp ]
     [ run array6.cpp unit_test_framework : : : : array6 ]
     [ run array7.cpp unit_test_framework : : : : array7 ]
+ [ run array_constexpr.cpp unit_test_framework : : : : array_constexpr ]
     [ compile-fail array_getfail1.cpp ]
     [ compile-fail array_getfail2.cpp ]
     [ run array_hash.cpp unit_test_framework : : : : array_hash ]

Added: trunk/libs/array/test/array_constexpr.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/array/test/array_constexpr.cpp 2013-02-12 13:07:15 EST (Tue, 12 Feb 2013)
@@ -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
+


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk