From: Karl Ljungkvist Date: Fri, 27 Jan 2017 17:08:57 +0000 (+0100) Subject: add new macro for static asserts X-Git-Tag: v8.5.0-rc1~179^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c9283e795004308c570f356da95f97562f62d112;p=dealii.git add new macro for static asserts static_assert is only supported from C++11 and onwards, so we need to guard the definition. Since this is a *declaration* rather than a statement, it does not behave like other Assert* macros. We therefore name it differently. --- diff --git a/include/deal.II/base/exceptions.h b/include/deal.II/base/exceptions.h index 5dfca28ebc..c0ed32c46d 100644 --- a/include/deal.II/base/exceptions.h +++ b/include/deal.II/base/exceptions.h @@ -1228,6 +1228,18 @@ namespace StandardExceptions dealii::ExcCudaError(cudaGetErrorString(error_code))) #endif +// A static assert which checks a compile-time condition. This is nothing more +// than a wrapper around the C++11 static_assert declaration, which +// we need to disable for non-C++11. + +// Just like static_assert, it takes two arguments, a +// constexpr condition and an error message. +#if defined(DEAL_II_WITH_CXX11) +#define DEAL_II_STATIC_ASSERT(...) static_assert(__VA_ARGS__) +#else +#define DEAL_II_STATIC_ASSERT(...) /* do nothing */ +#endif + using namespace StandardExceptions; DEAL_II_NAMESPACE_CLOSE diff --git a/include/deal.II/base/quadrature_point_data.h b/include/deal.II/base/quadrature_point_data.h index 80c603dc6b..f6bc31ff2e 100644 --- a/include/deal.II/base/quadrature_point_data.h +++ b/include/deal.II/base/quadrature_point_data.h @@ -21,6 +21,7 @@ #ifdef DEAL_II_WITH_CXX11 +#include #include #include #include @@ -326,8 +327,8 @@ namespace parallel class ContinuousQuadratureDataTransfer { public: - static_assert(std::is_base_of::value, - "User's DataType class should be derived from QPData"); + DEAL_II_STATIC_ASSERT(std::is_base_of::value, + "User's DataType class should be derived from QPData"); /** * A typedef for a cell. @@ -505,8 +506,8 @@ template void CellDataStorage::initialize(const CellIteratorType &cell, const unsigned int n_q_points) { - static_assert(std::is_base_of::value, - "User's T class should be derived from user's DataType class"); + DEAL_II_STATIC_ASSERT(std::is_base_of::value, + "User's T class should be derived from user's DataType class"); if (map.find(cell) == map.end()) { @@ -577,8 +578,8 @@ template std::vector > CellDataStorage::get_data(const CellIteratorType &cell) { - static_assert(std::is_base_of::value, - "User's T class should be derived from user's DataType class"); + DEAL_II_STATIC_ASSERT(std::is_base_of::value, + "User's T class should be derived from user's DataType class"); auto it = map.find(cell); Assert(it != map.end(), ExcMessage("Could not find data for the cell")); @@ -601,8 +602,8 @@ template std::vector > CellDataStorage::get_data(const CellIteratorType &cell) const { - static_assert(std::is_base_of::value, - "User's T class should be derived from user's DataType class"); + DEAL_II_STATIC_ASSERT(std::is_base_of::value, + "User's T class should be derived from user's DataType class"); auto it = map.find(cell); Assert(it != map.end(), ExcMessage("Could not find QP data for the cell")); @@ -634,8 +635,8 @@ void pack_cell_data const CellDataStorage *data_storage, FullMatrix &matrix_data) { - static_assert(std::is_base_of::value, - "User's DataType class should be derived from QPData"); + DEAL_II_STATIC_ASSERT(std::is_base_of::value, + "User's DataType class should be derived from QPData"); const std::vector > qpd = data_storage->get_data(cell); @@ -667,8 +668,8 @@ void unpack_to_cell_data const FullMatrix &values_at_qp, CellDataStorage *data_storage) { - static_assert(std::is_base_of::value, - "User's DataType class should be derived from QPData"); + DEAL_II_STATIC_ASSERT(std::is_base_of::value, + "User's DataType class should be derived from QPData"); std::vector > qpd = data_storage->get_data(cell); diff --git a/include/deal.II/base/tensor_accessors.h b/include/deal.II/base/tensor_accessors.h index f366cd415f..3bec92b90e 100644 --- a/include/deal.II/base/tensor_accessors.h +++ b/include/deal.II/base/tensor_accessors.h @@ -17,6 +17,7 @@ #define dealii__tensor_accessors_h #include +#include #include #include @@ -185,10 +186,8 @@ namespace TensorAccessors internal::ReorderedIndexView reordered_index_view(T &t) { -#ifdef DEAL_II_WITH_CXX11 - static_assert(0 <= index && index < rank, - "The specified index must lie within the range [0,rank)"); -#endif + DEAL_II_STATIC_ASSERT(0 <= index && index < rank, + "The specified index must lie within the range [0,rank)"); return internal::ReorderedIndexView(t); } @@ -266,14 +265,12 @@ namespace TensorAccessors inline DEAL_II_ALWAYS_INLINE void contract(T1 &result, const T2 &left, const T3 &right) { -#ifdef DEAL_II_WITH_CXX11 - static_assert(rank_1 >= no_contr, "The rank of the left tensor must be " - "equal or greater than the number of " - "contractions"); - static_assert(rank_2 >= no_contr, "The rank of the right tensor must be " - "equal or greater than the number of " - "contractions"); -#endif + DEAL_II_STATIC_ASSERT(rank_1 >= no_contr, "The rank of the left tensor must be " + "equal or greater than the number of " + "contractions"); + DEAL_II_STATIC_ASSERT(rank_2 >= no_contr, "The rank of the right tensor must be " + "equal or greater than the number of " + "contractions"); internal::Contract::template contract (result, left, right); diff --git a/include/deal.II/dofs/dof_handler.h b/include/deal.II/dofs/dof_handler.h index da182ff831..5539e5d9d8 100644 --- a/include/deal.II/dofs/dof_handler.h +++ b/include/deal.II/dofs/dof_handler.h @@ -1096,12 +1096,10 @@ private: // explicitly check for sensible template arguments, but not on windows // because MSVC creates bogus warnings during normal compilation -#ifdef DEAL_II_WITH_CXX11 #ifndef DEAL_II_MSVC - static_assert (dim<=spacedim, - "The dimension of a DoFHandler must be less than or " - "equal to the space dimension in which it lives."); -#endif + DEAL_II_STATIC_ASSERT (dim<=spacedim, + "The dimension of a DoFHandler must be less than or " + "equal to the space dimension in which it lives."); #endif }; diff --git a/include/deal.II/fe/fe.h b/include/deal.II/fe/fe.h index 3b8f176cd9..ddef94a55b 100644 --- a/include/deal.II/fe/fe.h +++ b/include/deal.II/fe/fe.h @@ -2725,12 +2725,10 @@ protected: // explicitly check for sensible template arguments, but not on windows // because MSVC creates bogus warnings during normal compilation -#ifdef DEAL_II_WITH_CXX11 #ifndef DEAL_II_MSVC - static_assert (dim<=spacedim, - "The dimension of a FiniteElement must be less than or " - "equal to the space dimension in which it lives."); -#endif + DEAL_II_STATIC_ASSERT (dim<=spacedim, + "The dimension of a FiniteElement must be less than or " + "equal to the space dimension in which it lives."); #endif }; diff --git a/include/deal.II/grid/manifold.h b/include/deal.II/grid/manifold.h index 8e97d71509..8cfc3f899c 100644 --- a/include/deal.II/grid/manifold.h +++ b/include/deal.II/grid/manifold.h @@ -299,11 +299,9 @@ class Manifold : public Subscriptor public: // explicitly check for sensible template arguments -#ifdef DEAL_II_WITH_CXX11 - static_assert (dim<=spacedim, - "The dimension of a Manifold must be less than or " - "equal to the space dimension in which it lives."); -#endif + DEAL_II_STATIC_ASSERT (dim<=spacedim, + "The dimension of a Manifold must be less than or " + "equal to the space dimension in which it lives."); /** @@ -888,11 +886,9 @@ class ChartManifold : public Manifold { public: // explicitly check for sensible template arguments -#ifdef DEAL_II_WITH_CXX11 - static_assert (dim<=spacedim, - "The dimension of a ChartManifold must be less than or " - "equal to the space dimension in which it lives."); -#endif + DEAL_II_STATIC_ASSERT (dim<=spacedim, + "The dimension of a ChartManifold must be less than or " + "equal to the space dimension in which it lives."); /** * Constructor. The optional argument can be used to specify the periodicity diff --git a/include/deal.II/grid/tria.h b/include/deal.II/grid/tria.h index 7428feb138..f2a06b1638 100644 --- a/include/deal.II/grid/tria.h +++ b/include/deal.II/grid/tria.h @@ -3528,12 +3528,10 @@ private: // explicitly check for sensible template arguments, but not on windows // because MSVC creates bogus warnings during normal compilation -#ifdef DEAL_II_WITH_CXX11 #ifndef DEAL_II_MSVC - static_assert (dim<=spacedim, - "The dimension of a Triangulation must be less than or " - "equal to the space dimension in which it lives."); -#endif + DEAL_II_STATIC_ASSERT (dim<=spacedim, + "The dimension of a Triangulation must be less than or " + "equal to the space dimension in which it lives."); #endif }; diff --git a/include/deal.II/lac/block_linear_operator.h b/include/deal.II/lac/block_linear_operator.h index 3ca3496061..3850ada4f4 100644 --- a/include/deal.II/lac/block_linear_operator.h +++ b/include/deal.II/lac/block_linear_operator.h @@ -502,8 +502,8 @@ template block_operator(const std::array, n>, m> &ops) { - static_assert(m > 0 && n > 0, - "a blocked LinearOperator must consist of at least one block"); + DEAL_II_STATIC_ASSERT(m > 0 && n > 0, + "a blocked LinearOperator must consist of at least one block"); typedef typename BlockLinearOperator::BlockType BlockType; @@ -611,8 +611,8 @@ template BlockLinearOperator block_diagonal_operator(const std::array, m> &ops) { - static_assert(m > 0, - "a blockdiagonal LinearOperator must consist of at least one block"); + DEAL_II_STATIC_ASSERT(m > 0, + "a blockdiagonal LinearOperator must consist of at least one block"); typedef typename BlockLinearOperator::BlockType BlockType; @@ -655,9 +655,9 @@ template BlockLinearOperator block_diagonal_operator(const LinearOperator &op) { - static_assert(m > 0, - "a blockdiagonal LinearOperator must consist of at least " - "one block"); + DEAL_II_STATIC_ASSERT(m > 0, + "a blockdiagonal LinearOperator must consist of at least " + "one block"); typedef typename BlockLinearOperator::BlockType BlockType; std::array new_ops; diff --git a/include/deal.II/lac/block_vector_base.h b/include/deal.II/lac/block_vector_base.h index 69506a8ef0..01d47e2257 100644 --- a/include/deal.II/lac/block_vector_base.h +++ b/include/deal.II/lac/block_vector_base.h @@ -1044,9 +1044,9 @@ namespace internal // true). As mentioned above, try to check this at compile time if C++11 // support is available. #ifdef DEAL_II_WITH_CXX11 - static_assert(Constness == true, - "Constructing a non-const iterator from a const iterator " - "does not make sense."); + DEAL_II_STATIC_ASSERT(Constness == true, + "Constructing a non-const iterator from a const iterator " + "does not make sense."); #else Assert(Constness == true, ExcCastingAwayConstness()); #endif diff --git a/include/deal.II/lac/linear_operator.h b/include/deal.II/lac/linear_operator.h index 0fbfec3161..0d82dd719e 100644 --- a/include/deal.II/lac/linear_operator.h +++ b/include/deal.II/lac/linear_operator.h @@ -466,7 +466,7 @@ LinearOperator operator*(typename Range::value_type number, const LinearOperator &op) { - static_assert( + DEAL_II_STATIC_ASSERT( std::is_convertible::value, "Range and Domain must have implicitly convertible 'value_type's"); @@ -536,7 +536,7 @@ LinearOperator operator*(const LinearOperator &op, typename Domain::value_type number) { - static_assert( + DEAL_II_STATIC_ASSERT( std::is_convertible::value, "Range and Domain must have implicitly convertible 'value_type's"); diff --git a/include/deal.II/lac/precondition.h b/include/deal.II/lac/precondition.h index c201cf15cd..5c76405344 100644 --- a/include/deal.II/lac/precondition.h +++ b/include/deal.II/lac/precondition.h @@ -19,6 +19,7 @@ // This file contains simple preconditioners. #include +#include #include #include #include @@ -1254,11 +1255,9 @@ template inline void PreconditionRichardson::vmult (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 - static_assert( + DEAL_II_STATIC_ASSERT( std::is_same::value, "PreconditionRichardson and VectorType must have the same size_type."); -#endif // DEAL_II_WITH_CXX11 dst.equ(relaxation,src); } @@ -1269,11 +1268,9 @@ template inline void PreconditionRichardson::Tvmult (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 - static_assert( + DEAL_II_STATIC_ASSERT( std::is_same::value, "PreconditionRichardson and VectorType must have the same size_type."); -#endif // DEAL_II_WITH_CXX11 dst.equ(relaxation,src); } @@ -1282,11 +1279,9 @@ template inline void PreconditionRichardson::vmult_add (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 - static_assert( + DEAL_II_STATIC_ASSERT( std::is_same::value, "PreconditionRichardson and VectorType must have the same size_type."); -#endif // DEAL_II_WITH_CXX11 dst.add(relaxation,src); } @@ -1297,11 +1292,9 @@ template inline void PreconditionRichardson::Tvmult_add (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 - static_assert( + DEAL_II_STATIC_ASSERT( std::is_same::value, "PreconditionRichardson and VectorType must have the same size_type."); -#endif // DEAL_II_WITH_CXX11 dst.add(relaxation,src); } @@ -1362,11 +1355,9 @@ template inline void PreconditionJacobi::vmult (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 - static_assert( + DEAL_II_STATIC_ASSERT( std::is_same::size_type, typename VectorType::size_type>::value, "PreconditionJacobi and VectorType must have the same size_type."); -#endif // DEAL_II_WITH_CXX11 Assert (this->A!=0, ExcNotInitialized()); this->A->precondition_Jacobi (dst, src, this->relaxation); @@ -1379,11 +1370,9 @@ template inline void PreconditionJacobi::Tvmult (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 - static_assert( + DEAL_II_STATIC_ASSERT( std::is_same::size_type, typename VectorType::size_type>::value, "PreconditionJacobi and VectorType must have the same size_type."); -#endif // DEAL_II_WITH_CXX11 Assert (this->A!=0, ExcNotInitialized()); this->A->precondition_Jacobi (dst, src, this->relaxation); @@ -1396,11 +1385,9 @@ template inline void PreconditionJacobi::step (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 - static_assert( + DEAL_II_STATIC_ASSERT( std::is_same::size_type, typename VectorType::size_type>::value, "PreconditionJacobi and VectorType must have the same size_type."); -#endif // DEAL_II_WITH_CXX11 Assert (this->A!=0, ExcNotInitialized()); this->A->Jacobi_step (dst, src, this->relaxation); @@ -1413,11 +1400,9 @@ template inline void PreconditionJacobi::Tstep (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 - static_assert( + DEAL_II_STATIC_ASSERT( std::is_same::size_type, typename VectorType::size_type>::value, "PreconditionJacobi and VectorType must have the same size_type."); -#endif // DEAL_II_WITH_CXX11 step (dst, src); } @@ -1431,11 +1416,9 @@ template inline void PreconditionSOR::vmult (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 - static_assert( + DEAL_II_STATIC_ASSERT( std::is_same::size_type, typename VectorType::size_type>::value, "PreconditionSOR and VectorType must have the same size_type."); -#endif // DEAL_II_WITH_CXX11 Assert (this->A!=0, ExcNotInitialized()); this->A->precondition_SOR (dst, src, this->relaxation); @@ -1448,11 +1431,9 @@ template inline void PreconditionSOR::Tvmult (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 - static_assert( + DEAL_II_STATIC_ASSERT( std::is_same::size_type, typename VectorType::size_type>::value, "PreconditionSOR and VectorType must have the same size_type."); -#endif // DEAL_II_WITH_CXX11 Assert (this->A!=0, ExcNotInitialized()); this->A->precondition_TSOR (dst, src, this->relaxation); @@ -1465,11 +1446,9 @@ template inline void PreconditionSOR::step (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 - static_assert( + DEAL_II_STATIC_ASSERT( std::is_same::size_type, typename VectorType::size_type>::value, "PreconditionSOR and VectorType must have the same size_type."); -#endif // DEAL_II_WITH_CXX11 Assert (this->A!=0, ExcNotInitialized()); this->A->SOR_step (dst, src, this->relaxation); @@ -1482,11 +1461,9 @@ template inline void PreconditionSOR::Tstep (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 - static_assert( + DEAL_II_STATIC_ASSERT( std::is_same::size_type, typename VectorType::size_type>::value, "PreconditionSOR and VectorType must have the same size_type."); -#endif // DEAL_II_WITH_CXX11 Assert (this->A!=0, ExcNotInitialized()); this->A->TSOR_step (dst, src, this->relaxation); @@ -1535,11 +1512,9 @@ template inline void PreconditionSSOR::vmult (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 - static_assert( + DEAL_II_STATIC_ASSERT( std::is_same::size_type, typename VectorType::size_type>::value, "PreconditionSSOR and VectorType must have the same size_type."); -#endif // DEAL_II_WITH_CXX11 Assert (this->A!=0, ExcNotInitialized()); this->A->precondition_SSOR (dst, src, this->relaxation, pos_right_of_diagonal); @@ -1552,11 +1527,9 @@ template inline void PreconditionSSOR::Tvmult (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 - static_assert( + DEAL_II_STATIC_ASSERT( std::is_same::size_type, typename VectorType::size_type>::value, "PreconditionSSOR and VectorType must have the same size_type."); -#endif // DEAL_II_WITH_CXX11 Assert (this->A!=0, ExcNotInitialized()); this->A->precondition_SSOR (dst, src, this->relaxation, pos_right_of_diagonal); @@ -1569,11 +1542,9 @@ template inline void PreconditionSSOR::step (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 - static_assert( + DEAL_II_STATIC_ASSERT( std::is_same::size_type, typename VectorType::size_type>::value, "PreconditionSSOR and VectorType must have the same size_type."); -#endif // DEAL_II_WITH_CXX11 Assert (this->A!=0, ExcNotInitialized()); this->A->SSOR_step (dst, src, this->relaxation); @@ -1586,11 +1557,9 @@ template inline void PreconditionSSOR::Tstep (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 - static_assert( + DEAL_II_STATIC_ASSERT( std::is_same::size_type, typename VectorType::size_type>::value, "PreconditionSSOR and VectorType must have the same size_type."); -#endif // DEAL_II_WITH_CXX11 step (dst, src); } @@ -1630,11 +1599,9 @@ template inline void PreconditionPSOR::vmult (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 - static_assert( + DEAL_II_STATIC_ASSERT( std::is_same::size_type, typename VectorType::size_type>::value, "PreconditionPSOR and VectorType must have the same size_type."); -#endif // DEAL_II_WITH_CXX11 Assert (this->A!=0, ExcNotInitialized()); dst = src; @@ -1648,11 +1615,9 @@ template inline void PreconditionPSOR::Tvmult (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 - static_assert( + DEAL_II_STATIC_ASSERT( std::is_same::size_type, typename VectorType::size_type>::value, "PreconditionPSOR and VectorType must have the same size_type."); -#endif // DEAL_II_WITH_CXX11 Assert (this->A!=0, ExcNotInitialized()); dst = src; @@ -2016,11 +1981,9 @@ PreconditionChebyshev::PreconditionChe delta (1.), is_initialized (false) { -#ifdef DEAL_II_WITH_CXX11 - static_assert( + DEAL_II_STATIC_ASSERT( std::is_same::value, "PreconditionChebyshev and VectorType must have the same size_type."); -#endif // DEAL_II_WITH_CXX11 } diff --git a/include/deal.II/lac/trilinos_block_sparse_matrix.h b/include/deal.II/lac/trilinos_block_sparse_matrix.h index c30a7230d9..15eb2163e8 100644 --- a/include/deal.II/lac/trilinos_block_sparse_matrix.h +++ b/include/deal.II/lac/trilinos_block_sparse_matrix.h @@ -608,8 +608,8 @@ namespace TrilinosWrappers template TrilinosBlockPayload (const Args &...) { - static_assert(typeid(PayloadBlockType)==typeid(internal::LinearOperator::TrilinosPayload), - "TrilinosBlockPayload can only accept a payload of type TrilinosPayload."); + DEAL_II_STATIC_ASSERT(typeid(PayloadBlockType)==typeid(internal::LinearOperator::TrilinosPayload), + "TrilinosBlockPayload can only accept a payload of type TrilinosPayload."); } }; diff --git a/include/deal.II/matrix_free/operators.h b/include/deal.II/matrix_free/operators.h index fa27b7bee1..61706fd28f 100644 --- a/include/deal.II/matrix_free/operators.h +++ b/include/deal.II/matrix_free/operators.h @@ -1097,13 +1097,9 @@ namespace MatrixFreeOperators MGInterfaceOperator::vmult (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 -#ifndef DEAL_II_MSVC - static_assert (std::is_same::value, - "The vector type must be based on the same value type as this" - "operator"); -#endif -#endif + DEAL_II_STATIC_ASSERT (std::is_same::value, + "The vector type must be based on the same value type as this" + "operator"); Assert(mf_base_operator != NULL, ExcNotInitialized()); @@ -1119,13 +1115,9 @@ namespace MatrixFreeOperators MGInterfaceOperator::Tvmult (VectorType &dst, const VectorType &src) const { -#ifdef DEAL_II_WITH_CXX11 -#ifndef DEAL_II_MSVC - static_assert (std::is_same::value, - "The vector type must be based on the same value type as this" - "operator"); -#endif -#endif + DEAL_II_STATIC_ASSERT (std::is_same::value, + "The vector type must be based on the same value type as this" + "operator"); Assert(mf_base_operator != NULL, ExcNotInitialized());