Eigen  5.0.1
 
Loading...
Searching...
No Matches
StaticAssert.h
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// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_STATIC_ASSERT_H
13#define EIGEN_STATIC_ASSERT_H
14
15/* Some notes on Eigen's static assertion mechanism:
16 *
17 * - in EIGEN_STATIC_ASSERT(CONDITION,MSG) the parameter CONDITION must be a compile time boolean
18 * expression, and MSG an identifier that is stringified into the diagnostic; by convention it is written
19 * ALL_CAPS. Prefer one of the messages already used by the derived EIGEN_STATIC_ASSERT_* macros below.
20 *
21 * - EIGEN_STATIC_ASSERT expands to a plain static_assert, so it may be used wherever a declaration is allowed,
22 * including class scope (see e.g. log1p_impl in MathFunctions.h). That is why EIGEN_NO_STATIC_ASSERT removes
23 * the check outright instead of downgrading it to an eigen_assert, which would only be valid in function scope.
24 *
25 */
26
27#ifndef EIGEN_STATIC_ASSERT
28#ifndef EIGEN_NO_STATIC_ASSERT
29
30#define EIGEN_STATIC_ASSERT(X, MSG) static_assert(X, #MSG);
31
32#else // EIGEN_NO_STATIC_ASSERT
33
34#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
35
36#endif // EIGEN_NO_STATIC_ASSERT
37#endif // EIGEN_STATIC_ASSERT
38
39// static assertion failing if the type \a TYPE is not a vector type
40#define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE) \
41 EIGEN_STATIC_ASSERT(TYPE::IsVectorAtCompileTime, YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX)
42
43// static assertion failing if the type \a TYPE is not fixed-size
44#define EIGEN_STATIC_ASSERT_FIXED_SIZE(TYPE) \
45 EIGEN_STATIC_ASSERT(TYPE::SizeAtCompileTime != Eigen::Dynamic, \
46 YOU_CALLED_A_FIXED_SIZE_METHOD_ON_A_DYNAMIC_SIZE_MATRIX_OR_VECTOR)
47
48// static assertion failing if the type \a TYPE is not dynamic-size
49#define EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(TYPE) \
50 EIGEN_STATIC_ASSERT(TYPE::SizeAtCompileTime == Eigen::Dynamic, \
51 YOU_CALLED_A_DYNAMIC_SIZE_METHOD_ON_A_FIXED_SIZE_MATRIX_OR_VECTOR)
52
53// static assertion failing if the type \a TYPE is not a vector type of the given size
54#define EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(TYPE, SIZE) \
55 EIGEN_STATIC_ASSERT(TYPE::IsVectorAtCompileTime&& TYPE::SizeAtCompileTime == SIZE, \
56 THIS_METHOD_IS_ONLY_FOR_VECTORS_OF_A_SPECIFIC_SIZE)
57
58// static assertion failing if the type \a TYPE is not a vector type of the given size
59#define EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(TYPE, ROWS, COLS) \
60 EIGEN_STATIC_ASSERT(TYPE::RowsAtCompileTime == ROWS && TYPE::ColsAtCompileTime == COLS, \
61 THIS_METHOD_IS_ONLY_FOR_MATRICES_OF_A_SPECIFIC_SIZE)
62
63// static assertion failing if the two vector expression types are not compatible (same fixed-size or dynamic size)
64#define EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(TYPE0, TYPE1) \
65 EIGEN_STATIC_ASSERT( \
66 (int(TYPE0::SizeAtCompileTime) == Eigen::Dynamic || int(TYPE1::SizeAtCompileTime) == Eigen::Dynamic || \
67 int(TYPE0::SizeAtCompileTime) == int(TYPE1::SizeAtCompileTime)), \
68 YOU_MIXED_VECTORS_OF_DIFFERENT_SIZES)
69
70#define EIGEN_PREDICATE_SAME_MATRIX_SIZE(TYPE0, TYPE1) \
71 ((int(Eigen::internal::size_of_xpr_at_compile_time<TYPE0>::value) == 0 && \
72 int(Eigen::internal::size_of_xpr_at_compile_time<TYPE1>::value) == 0) || \
73 ((int(TYPE0::RowsAtCompileTime) == Eigen::Dynamic || int(TYPE1::RowsAtCompileTime) == Eigen::Dynamic || \
74 int(TYPE0::RowsAtCompileTime) == int(TYPE1::RowsAtCompileTime)) && \
75 (int(TYPE0::ColsAtCompileTime) == Eigen::Dynamic || int(TYPE1::ColsAtCompileTime) == Eigen::Dynamic || \
76 int(TYPE0::ColsAtCompileTime) == int(TYPE1::ColsAtCompileTime))))
77
78#define EIGEN_STATIC_ASSERT_NON_INTEGER(TYPE) \
79 EIGEN_STATIC_ASSERT(!Eigen::NumTraits<TYPE>::IsInteger, THIS_FUNCTION_IS_NOT_FOR_INTEGER_NUMERIC_TYPES)
80
81// static assertion failing if it is guaranteed at compile-time that the two matrix expression types have different
82// sizes
83#define EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(TYPE0, TYPE1) \
84 EIGEN_STATIC_ASSERT(EIGEN_PREDICATE_SAME_MATRIX_SIZE(TYPE0, TYPE1), YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES)
85
86#define EIGEN_STATIC_ASSERT_SIZE_1x1(TYPE) \
87 EIGEN_STATIC_ASSERT((TYPE::RowsAtCompileTime == 1 || TYPE::RowsAtCompileTime == Eigen::Dynamic) && \
88 (TYPE::ColsAtCompileTime == 1 || TYPE::ColsAtCompileTime == Eigen::Dynamic), \
89 THIS_METHOD_IS_ONLY_FOR_1x1_EXPRESSIONS)
90
91#define EIGEN_STATIC_ASSERT_LVALUE(Derived) \
92 EIGEN_STATIC_ASSERT(Eigen::internal::is_lvalue<Derived>::value, THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY)
93
94#define EIGEN_STATIC_ASSERT_ARRAYXPR(Derived) \
95 EIGEN_STATIC_ASSERT((std::is_same<typename Eigen::internal::traits<Derived>::XprKind, ArrayXpr>::value), \
96 THIS_METHOD_IS_ONLY_FOR_ARRAYS_NOT_MATRICES)
97
98#define EIGEN_STATIC_ASSERT_SAME_XPR_KIND(Derived1, Derived2) \
99 EIGEN_STATIC_ASSERT((std::is_same<typename Eigen::internal::traits<Derived1>::XprKind, \
100 typename Eigen::internal::traits<Derived2>::XprKind>::value), \
101 YOU_CANNOT_MIX_ARRAYS_AND_MATRICES)
102
103// Check that a cost value is positive, and that is stay within a reasonable range
104// TODO this check could be enabled for internal debugging only
105#define EIGEN_INTERNAL_CHECK_COST_VALUE(C) \
106 EIGEN_STATIC_ASSERT((C) >= 0 && (C) <= HugeCost * HugeCost, \
107 EIGEN_INTERNAL_ERROR_PLEASE_FILE_A_BUG_REPORT__INVALID_COST_VALUE);
108
109#endif // EIGEN_STATIC_ASSERT_H