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