StaticAssert.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_STATIC_ASSERT_H
12 #define EIGEN_STATIC_ASSERT_H
13 
14 /* Some notes on Eigen's static assertion mechanism:
15  *
16  * - in EIGEN_STATIC_ASSERT(CONDITION,MSG) the parameter CONDITION must be a compile time boolean
17  * expression, and MSG an enum listed in struct internal::static_assertion<true>
18  *
19  * - define EIGEN_NO_STATIC_ASSERT to disable them (and save compilation time)
20  * in that case, the static assertion is converted to the following runtime assert:
21  * eigen_assert(CONDITION && "MSG")
22  *
23  * - currently EIGEN_STATIC_ASSERT can only be used in function scope
24  *
25  */
26 
27 #ifndef EIGEN_NO_STATIC_ASSERT
28 
29  #if EIGEN_MAX_CPP_VER>=11 && (__has_feature(cxx_static_assert) || (defined(__cplusplus) && __cplusplus >= 201103L) || (EIGEN_COMP_MSVC >= 1600))
30 
31  // if native static_assert is enabled, let's use it
32  #define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);
33 
34  #else // not CXX0X
35 
36  namespace Eigen {
37 
38  namespace internal {
39 
40  template<bool condition>
41  struct static_assertion {};
42 
43  template<>
44  struct static_assertion<true>
45  {
46  enum {
104  CHOLMOD_SUPPORTS_DOUBLE_PRECISION_ONLY
105  };
106  };
107 
108  } // end namespace internal
109 
110  } // end namespace Eigen
111 
112  // Specialized implementation for MSVC to avoid "conditional
113  // expression is constant" warnings. This implementation doesn't
114  // appear to work under GCC, hence the multiple implementations.
115  #if EIGEN_COMP_MSVC
116 
117  #define EIGEN_STATIC_ASSERT(CONDITION,MSG) \
118  {Eigen::internal::static_assertion<bool(CONDITION)>::MSG;}
119 
120  #else
121  // In some cases clang interprets bool(CONDITION) as function declaration
122  #define EIGEN_STATIC_ASSERT(CONDITION,MSG) \
123  if (Eigen::internal::static_assertion<static_cast<bool>(CONDITION)>::MSG) {}
124 
125  #endif
126 
127  #endif // not CXX0X
128 
129 #else // EIGEN_NO_STATIC_ASSERT
130 
131  #define EIGEN_STATIC_ASSERT(CONDITION,MSG) eigen_assert((CONDITION) && #MSG);
132 
133 #endif // EIGEN_NO_STATIC_ASSERT
134 
135 
136 // static assertion failing if the type \a TYPE is not a vector type
137 #define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE) \
138  EIGEN_STATIC_ASSERT(TYPE::IsVectorAtCompileTime, \
139  YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX)
140 
141 // static assertion failing if the type \a TYPE is not fixed-size
142 #define EIGEN_STATIC_ASSERT_FIXED_SIZE(TYPE) \
143  EIGEN_STATIC_ASSERT(TYPE::SizeAtCompileTime!=Eigen::Dynamic, \
144  YOU_CALLED_A_FIXED_SIZE_METHOD_ON_A_DYNAMIC_SIZE_MATRIX_OR_VECTOR)
145 
146 // static assertion failing if the type \a TYPE is not dynamic-size
147 #define EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(TYPE) \
148  EIGEN_STATIC_ASSERT(TYPE::SizeAtCompileTime==Eigen::Dynamic, \
149  YOU_CALLED_A_DYNAMIC_SIZE_METHOD_ON_A_FIXED_SIZE_MATRIX_OR_VECTOR)
150 
151 // static assertion failing if the type \a TYPE is not a vector type of the given size
152 #define EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(TYPE, SIZE) \
153  EIGEN_STATIC_ASSERT(TYPE::IsVectorAtCompileTime && TYPE::SizeAtCompileTime==SIZE, \
154  THIS_METHOD_IS_ONLY_FOR_VECTORS_OF_A_SPECIFIC_SIZE)
155 
156 // static assertion failing if the type \a TYPE is not a vector type of the given size
157 #define EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(TYPE, ROWS, COLS) \
158  EIGEN_STATIC_ASSERT(TYPE::RowsAtCompileTime==ROWS && TYPE::ColsAtCompileTime==COLS, \
159  THIS_METHOD_IS_ONLY_FOR_MATRICES_OF_A_SPECIFIC_SIZE)
160 
161 // static assertion failing if the two vector expression types are not compatible (same fixed-size or dynamic size)
162 #define EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(TYPE0,TYPE1) \
163  EIGEN_STATIC_ASSERT( \
164  (int(TYPE0::SizeAtCompileTime)==Eigen::Dynamic \
165  || int(TYPE1::SizeAtCompileTime)==Eigen::Dynamic \
166  || int(TYPE0::SizeAtCompileTime)==int(TYPE1::SizeAtCompileTime)),\
167  YOU_MIXED_VECTORS_OF_DIFFERENT_SIZES)
168 
169 #define EIGEN_PREDICATE_SAME_MATRIX_SIZE(TYPE0,TYPE1) \
170  ( \
171  (int(Eigen::internal::size_of_xpr_at_compile_time<TYPE0>::ret)==0 && int(Eigen::internal::size_of_xpr_at_compile_time<TYPE1>::ret)==0) \
172  || (\
173  (int(TYPE0::RowsAtCompileTime)==Eigen::Dynamic \
174  || int(TYPE1::RowsAtCompileTime)==Eigen::Dynamic \
175  || int(TYPE0::RowsAtCompileTime)==int(TYPE1::RowsAtCompileTime)) \
176  && (int(TYPE0::ColsAtCompileTime)==Eigen::Dynamic \
177  || int(TYPE1::ColsAtCompileTime)==Eigen::Dynamic \
178  || int(TYPE0::ColsAtCompileTime)==int(TYPE1::ColsAtCompileTime))\
179  ) \
180  )
181 
182 #define EIGEN_STATIC_ASSERT_NON_INTEGER(TYPE) \
183  EIGEN_STATIC_ASSERT(!NumTraits<TYPE>::IsInteger, THIS_FUNCTION_IS_NOT_FOR_INTEGER_NUMERIC_TYPES)
184 
185 
186 // static assertion failing if it is guaranteed at compile-time that the two matrix expression types have different sizes
187 #define EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(TYPE0,TYPE1) \
188  EIGEN_STATIC_ASSERT( \
189  EIGEN_PREDICATE_SAME_MATRIX_SIZE(TYPE0,TYPE1),\
190  YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES)
191 
192 #define EIGEN_STATIC_ASSERT_SIZE_1x1(TYPE) \
193  EIGEN_STATIC_ASSERT((TYPE::RowsAtCompileTime == 1 || TYPE::RowsAtCompileTime == Dynamic) && \
194  (TYPE::ColsAtCompileTime == 1 || TYPE::ColsAtCompileTime == Dynamic), \
195  THIS_METHOD_IS_ONLY_FOR_1x1_EXPRESSIONS)
196 
197 #define EIGEN_STATIC_ASSERT_LVALUE(Derived) \
198  EIGEN_STATIC_ASSERT(Eigen::internal::is_lvalue<Derived>::value, \
199  THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY)
200 
201 #define EIGEN_STATIC_ASSERT_ARRAYXPR(Derived) \
202  EIGEN_STATIC_ASSERT((Eigen::internal::is_same<typename Eigen::internal::traits<Derived>::XprKind, ArrayXpr>::value), \
203  THIS_METHOD_IS_ONLY_FOR_ARRAYS_NOT_MATRICES)
204 
205 #define EIGEN_STATIC_ASSERT_SAME_XPR_KIND(Derived1, Derived2) \
206  EIGEN_STATIC_ASSERT((Eigen::internal::is_same<typename Eigen::internal::traits<Derived1>::XprKind, \
207  typename Eigen::internal::traits<Derived2>::XprKind \
208  >::value), \
209  YOU_CANNOT_MIX_ARRAYS_AND_MATRICES)
210 
211 // Check that a cost value is positive, and that is stay within a reasonable range
212 // TODO this check could be enabled for internal debugging only
213 #define EIGEN_INTERNAL_CHECK_COST_VALUE(C) \
214  EIGEN_STATIC_ASSERT((C)>=0 && (C)<=HugeCost*HugeCost, EIGEN_INTERNAL_ERROR_PLEASE_FILE_A_BUG_REPORT__INVALID_COST_VALUE);
215 
216 #endif // EIGEN_STATIC_ASSERT_H
Definition: LDLT.h:16


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:09:03