Assertions

Assertions

The macro eigen_assert is defined to be eigen_plain_assert by default. We use eigen_plain_assert instead of assert to work around a known bug for GCC <= 4.3. Basically, eigen_plain_assert is assert.

Redefining assertions

Both eigen_assert and eigen_plain_assert are defined in Macros.h. Defining eigen_assert indirectly gives you a chance to change its behavior. You can redefine this macro if you want to do something else such as throwing an exception, and fall back to its default behavior with eigen_plain_assert. The code below tells Eigen to throw an std::runtime_error:

#include <stdexcept>
#undef eigen_assert
#define eigen_assert(x) \
if (!(x)) { throw (std::runtime_error("Put your message here")); }

Disabling assertions

Assertions cost run time and can be turned off. You can suppress eigen_assert by defining EIGEN_NO_DEBUG before including Eigen headers. EIGEN_NO_DEBUG is undefined by default unless NDEBUG is defined.

Static assertions

Static assertions are not standardized until C++11. However, in the Eigen library, there are many conditions can and should be detectedat compile time. For instance, we use static assertions to prevent the code below from compiling.

Matrix3d() + Matrix4d(); // adding matrices of different sizes
Matrix4cd() * Vector3cd(); // invalid product known at compile time

Static assertions are defined in StaticAssert.h. If there is native static_assert, we use it. Otherwise, we have implemented an assertion macro that can show a limited range of messages.

One can easily come up with static assertions without messages, such as:

#define STATIC_ASSERT(x) \
switch(0) { case 0: case x:; }

However, the example above obviously cannot tell why the assertion failed. Therefore, we define a struct in namespace Eigen::internal to handle available messages.

template<bool condition>
struct static_assertion {};
template<>
struct static_assertion<true>
{
enum {
YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX,
YOU_MIXED_VECTORS_OF_DIFFERENT_SIZES,
// see StaticAssert.h for all enums.
};
};

And then, we define EIGEN_STATIC_ASSERT(CONDITION,MSG) to access Eigen::internal::static_assertion<bool(CONDITION)>::MSG. If the condition evaluates into false, your compiler displays a lot of messages explaining there is no MSG in static_assert<false>. Nevertheless, this is not in what we are interested. As you can see, all members of static_assert<true> are ALL_CAPS_AND_THEY_ARE_SHOUTING.

Warning
When using this macro, MSG should be a member of static_assertion<true>, or the static assertion always fails. Currently, it can only be used in function scope.

Derived static assertions

There are other macros derived from EIGEN_STATIC_ASSERT to enhance readability. Their names are self-explanatory.

Because Eigen handles both fixed-size and dynamic-size expressions, some conditions cannot be clearly determined at compile time. We classify them into strict assertions and permissive assertions.

Strict assertions

These assertions fail if the condition may not be met. For example, MatrixXd may not be a vector, so it fails EIGEN_STATIC_ASSERT_VECTOR_ONLY.

Permissive assertions

These assertions fail if the condition cannot be met. For example, MatrixXd and Matrix4d may have the same size, so they pass EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE.

See StaticAssert.h for details such as what messages they throw.

Disabling static assertions

If EIGEN_NO_STATIC_ASSERT is defined, static assertions turn into eigen_assert's, working like:

#define EIGEN_STATIC_ASSERT(CONDITION,MSG) eigen_assert((CONDITION) && #MSG);

This saves compile time but consumes more run time. EIGEN_NO_STATIC_ASSERT is undefined by default.



gtsam
Author(s):
autogenerated on Sat May 8 2021 02:51:44