Skip to content
Snippets Groups Projects
Commit 45990b69 authored by Martin Nolte's avatar Martin Nolte
Browse files

[c++11] add support for C++11 constexpr

As decided on the developer meeting in Aachen, we support (but do not
rely on) the C++11 keyword constexpr. This patch adds an m4-check for
constexpr and a header (cxx11.hh) defining a macro DUNE_CONSTEXPR either
as constexpr or empty (depending on the compiler support for it). It is
then used in FieldVector and FieldMatrix on size, rows, and cols.
parent b6e74464
Branches
Tags
No related merge requests found
......@@ -31,6 +31,7 @@ install(FILES
bitsetvector.hh
classname.hh
collectivecommunication.hh
cxx11.hh
debugallocator.hh
debugstream.hh
deprecated.hh
......
......@@ -28,6 +28,7 @@ commoninclude_HEADERS = \
bitsetvector.hh \
classname.hh \
collectivecommunication.hh \
cxx11.hh \
debugallocator.hh \
debugstream.hh \
deprecated.hh \
......
#ifndef DUNE_COMMON_CXX11_HH
#define DUNE_COMMON_CXX11_HH
#if HAVE_CONSTEXPR
#define DUNE_CONSTEXPR constexpr
#else // #if HAVE_CONSTEXPR
#define DUNE_CONSTEXPR
#endif // #else // #if HAVE_CONSTEXPR
#endif // #ifndef DUNE_COMMON_CXX11_HH
......@@ -8,6 +8,7 @@
#include <cstddef>
#include <iostream>
#include <dune/common/cxx11.hh>
#include <dune/common/exceptions.hh>
#include <dune/common/fvector.hh>
#include <dune/common/densematrix.hh>
......@@ -168,8 +169,8 @@ namespace Dune
}
// make this thing a matrix
size_type mat_rows() const { return ROWS; }
size_type mat_cols() const { return COLS; }
DUNE_CONSTEXPR size_type mat_rows() const { return ROWS; }
DUNE_CONSTEXPR size_type mat_cols() const { return COLS; }
row_reference mat_access ( size_type i )
{
......@@ -270,8 +271,8 @@ namespace Dune
}
// make this thing a matrix
size_type mat_rows() const { return 1; }
size_type mat_cols() const { return 1; }
DUNE_CONSTEXPR size_type mat_rows() const { return 1; }
DUNE_CONSTEXPR size_type mat_cols() const { return 1; }
row_reference mat_access ( size_type i )
{
......
......@@ -11,6 +11,8 @@
#include <cstring>
#include <utility>
#include <dune/common/cxx11.hh>
#include "typetraits.hh"
#include "exceptions.hh"
#include "array.hh"
......@@ -156,8 +158,10 @@ namespace Dune {
}
using Base::operator=;
DUNE_CONSTEXPR size_type size () const { return vec_size(); }
// make this thing a vector
size_type vec_size() const { return SIZE; }
DUNE_CONSTEXPR size_type vec_size () const { return SIZE; }
K & vec_access(size_type i) { return _data[i]; }
const K & vec_access(size_type i) const { return _data[i]; }
private:
......@@ -243,8 +247,10 @@ namespace Dune {
return *this;
}
DUNE_CONSTEXPR size_type size () const { return vec_size(); }
//===== forward methods to container
size_type vec_size() const { return 1; }
DUNE_CONSTEXPR size_type vec_size () const { return 1; }
K & vec_access(size_type i)
{
assert(i == 0);
......
# tests for C++11 constexpr support
# the associated macro is called HAVE_CONSTEXPR
AC_DEFUN([CXX11_CONSTEXPR_CHECK],[
AC_CACHE_CHECK([for C++11 constexpr], dune_cv_cxx11_constexpr_support, [
AC_REQUIRE([AC_PROG_CXX])
AC_REQUIRE([GXX0X])
AC_LANG_PUSH([C++])
AC_COMPILE_IFELSE([
AC_LANG_PROGRAM([
constexpr int foo () { return 0; }
template< int v >
struct A
{
static const int value = v;
};
],[
return A< foo() >::value;
])],
dune_cv_cxx11_constexpr_support=yes,
dune_cv_cxx11_constexpr_support=no)
AC_LANG_POP
])
if test "x$dune_cv_cxx11_constexpr_support" = xyes; then
AC_DEFINE(HAVE_CONSTEXPR, 1, [Define to 1 if C++11 constexpr is supported])
fi
])
......@@ -26,6 +26,7 @@ AC_DEFUN([DUNE_COMMON_CHECKS],
AC_REQUIRE([RVALUE_REFERENCES_CHECK])
AC_REQUIRE([INITIALIZER_LIST_CHECK])
AC_REQUIRE([CXX11_CONDITIONAL_CHECK])
AC_REQUIRE([CXX11_CONSTEXPR_CHECK])
AC_REQUIRE([DUNE_BOOST_BASE])
AC_REQUIRE([MAKE_SHARED])
AC_REQUIRE([DUNE_LINKCXX])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment