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  * - currently EIGEN_STATIC_ASSERT can only be used in function scope
20  *
21  */
22 
23 #ifndef EIGEN_STATIC_ASSERT
24 #ifndef EIGEN_NO_STATIC_ASSERT
25 
26 #define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);
27 
28 #else // EIGEN_NO_STATIC_ASSERT
29 
30 #define EIGEN_STATIC_ASSERT(CONDITION,MSG)
31 
32 #endif // EIGEN_NO_STATIC_ASSERT
33 #endif // EIGEN_STATIC_ASSERT
34 
35 // static assertion failing if the type \a TYPE is not a vector type
36 #define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE) \
37  EIGEN_STATIC_ASSERT(TYPE::IsVectorAtCompileTime, \
38  YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX)
39 
40 // static assertion failing if the type \a TYPE is not fixed-size
41 #define EIGEN_STATIC_ASSERT_FIXED_SIZE(TYPE) \
42  EIGEN_STATIC_ASSERT(TYPE::SizeAtCompileTime!=Eigen::Dynamic, \
43  YOU_CALLED_A_FIXED_SIZE_METHOD_ON_A_DYNAMIC_SIZE_MATRIX_OR_VECTOR)
44 
45 // static assertion failing if the type \a TYPE is not dynamic-size
46 #define EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(TYPE) \
47  EIGEN_STATIC_ASSERT(TYPE::SizeAtCompileTime==Eigen::Dynamic, \
48  YOU_CALLED_A_DYNAMIC_SIZE_METHOD_ON_A_FIXED_SIZE_MATRIX_OR_VECTOR)
49 
50 // static assertion failing if the type \a TYPE is not a vector type of the given size
51 #define EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(TYPE, SIZE) \
52  EIGEN_STATIC_ASSERT(TYPE::IsVectorAtCompileTime && TYPE::SizeAtCompileTime==SIZE, \
53  THIS_METHOD_IS_ONLY_FOR_VECTORS_OF_A_SPECIFIC_SIZE)
54 
55 // static assertion failing if the type \a TYPE is not a vector type of the given size
56 #define EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(TYPE, ROWS, COLS) \
57  EIGEN_STATIC_ASSERT(TYPE::RowsAtCompileTime==ROWS && TYPE::ColsAtCompileTime==COLS, \
58  THIS_METHOD_IS_ONLY_FOR_MATRICES_OF_A_SPECIFIC_SIZE)
59 
60 // static assertion failing if the two vector expression types are not compatible (same fixed-size or dynamic size)
61 #define EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(TYPE0,TYPE1) \
62  EIGEN_STATIC_ASSERT( \
63  (int(TYPE0::SizeAtCompileTime)==Eigen::Dynamic \
64  || int(TYPE1::SizeAtCompileTime)==Eigen::Dynamic \
65  || int(TYPE0::SizeAtCompileTime)==int(TYPE1::SizeAtCompileTime)),\
66  YOU_MIXED_VECTORS_OF_DIFFERENT_SIZES)
67 
68 #define EIGEN_PREDICATE_SAME_MATRIX_SIZE(TYPE0,TYPE1) \
69  ( \
70  (int(Eigen::internal::size_of_xpr_at_compile_time<TYPE0>::ret)==0 && int(Eigen::internal::size_of_xpr_at_compile_time<TYPE1>::ret)==0) \
71  || (\
72  (int(TYPE0::RowsAtCompileTime)==Eigen::Dynamic \
73  || int(TYPE1::RowsAtCompileTime)==Eigen::Dynamic \
74  || int(TYPE0::RowsAtCompileTime)==int(TYPE1::RowsAtCompileTime)) \
75  && (int(TYPE0::ColsAtCompileTime)==Eigen::Dynamic \
76  || int(TYPE1::ColsAtCompileTime)==Eigen::Dynamic \
77  || int(TYPE0::ColsAtCompileTime)==int(TYPE1::ColsAtCompileTime))\
78  ) \
79  )
80 
81 #define EIGEN_STATIC_ASSERT_NON_INTEGER(TYPE) \
82  EIGEN_STATIC_ASSERT(!Eigen::NumTraits<TYPE>::IsInteger, THIS_FUNCTION_IS_NOT_FOR_INTEGER_NUMERIC_TYPES)
83 
84 
85 // static assertion failing if it is guaranteed at compile-time that the two matrix expression types have different sizes
86 #define EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(TYPE0,TYPE1) \
87  EIGEN_STATIC_ASSERT( \
88  EIGEN_PREDICATE_SAME_MATRIX_SIZE(TYPE0,TYPE1),\
89  YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES)
90 
91 #define EIGEN_STATIC_ASSERT_SIZE_1x1(TYPE) \
92  EIGEN_STATIC_ASSERT((TYPE::RowsAtCompileTime == 1 || TYPE::RowsAtCompileTime == Eigen::Dynamic) && \
93  (TYPE::ColsAtCompileTime == 1 || TYPE::ColsAtCompileTime == Eigen::Dynamic), \
94  THIS_METHOD_IS_ONLY_FOR_1x1_EXPRESSIONS)
95 
96 #define EIGEN_STATIC_ASSERT_LVALUE(Derived) \
97  EIGEN_STATIC_ASSERT(Eigen::internal::is_lvalue<Derived>::value, \
98  THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY)
99 
100 #define EIGEN_STATIC_ASSERT_ARRAYXPR(Derived) \
101  EIGEN_STATIC_ASSERT((Eigen::internal::is_same<typename Eigen::internal::traits<Derived>::XprKind, ArrayXpr>::value), \
102  THIS_METHOD_IS_ONLY_FOR_ARRAYS_NOT_MATRICES)
103 
104 #define EIGEN_STATIC_ASSERT_SAME_XPR_KIND(Derived1, Derived2) \
105  EIGEN_STATIC_ASSERT((Eigen::internal::is_same<typename Eigen::internal::traits<Derived1>::XprKind, \
106  typename Eigen::internal::traits<Derived2>::XprKind \
107  >::value), \
108  YOU_CANNOT_MIX_ARRAYS_AND_MATRICES)
109 
110 // Check that a cost value is positive, and that is stay within a reasonable range
111 // TODO this check could be enabled for internal debugging only
112 #define EIGEN_INTERNAL_CHECK_COST_VALUE(C) \
113  EIGEN_STATIC_ASSERT((C)>=0 && (C)<=HugeCost*HugeCost, EIGEN_INTERNAL_ERROR_PLEASE_FILE_A_BUG_REPORT__INVALID_COST_VALUE);
114 
115 #endif // EIGEN_STATIC_ASSERT_H