You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
5.3 KiB

  1. namespace Eigen {
  2. /** \page TopicAssertions Assertions
  3. \b Table \b of \b contents
  4. - \ref PlainAssert
  5. - \ref RedefineAssert
  6. - \ref DisableAssert
  7. - \ref StaticAssert
  8. - \ref DerivedStaticAssert
  9. - \ref DisableStaticAssert
  10. \section PlainAssert Assertions
  11. The macro eigen_assert is defined to be \c eigen_plain_assert by default. We use eigen_plain_assert instead of \c assert to work around a known bug for GCC <= 4.3. Basically, eigen_plain_assert \a is \c assert.
  12. \subsection RedefineAssert Redefining assertions
  13. 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:
  14. \code
  15. #include <stdexcept>
  16. #undef eigen_assert
  17. #define eigen_assert(x) \
  18. if (!x) { throw (std::runtime_error("Put your message here")); }
  19. \endcode
  20. \subsection DisableAssert Disabling assertions
  21. Assertions cost run time and can be turned off. You can suppress eigen_assert by defining \c EIGEN_NO_DEBUG \b before including Eigen headers. \c EIGEN_NO_DEBUG is undefined by default unless \c NDEBUG is defined.
  22. \section StaticAssert Static assertions
  23. 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.
  24. \code
  25. Matrix3d() + Matrix4d(); // adding matrices of different sizes
  26. Matrix4cd() * Vector3cd(); // invalid product known at compile time
  27. \endcode
  28. 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.
  29. One can easily come up with static assertions without messages, such as:
  30. \code
  31. #define STATIC_ASSERT(x) \
  32. switch(0) { case 0: case x:; }
  33. \endcode
  34. However, the example above obviously cannot tell why the assertion failed. Therefore, we define a \c struct in namespace Eigen::internal to handle available messages.
  35. \code
  36. template<bool condition>
  37. struct static_assertion {};
  38. template<>
  39. struct static_assertion<true>
  40. {
  41. enum {
  42. YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX,
  43. YOU_MIXED_VECTORS_OF_DIFFERENT_SIZES,
  44. // see StaticAssert.h for all enums.
  45. };
  46. };
  47. \endcode
  48. And then, we define EIGEN_STATIC_ASSERT(CONDITION,MSG) to access Eigen::internal::static_assertion<bool(CONDITION)>::MSG. If the condition evaluates into \c false, your compiler displays a lot of messages explaining there is no MSG in static_assert<false>. Nevertheless, this is \a not in what we are interested. As you can see, all members of static_assert<true> are ALL_CAPS_AND_THEY_ARE_SHOUTING.
  49. \warning
  50. When using this macro, MSG should be a member of static_assertion<true>, or the static assertion \b always fails.
  51. Currently, it can only be used in function scope.
  52. \subsection DerivedStaticAssert Derived static assertions
  53. There are other macros derived from EIGEN_STATIC_ASSERT to enhance readability. Their names are self-explanatory.
  54. - \b EIGEN_STATIC_ASSERT_FIXED_SIZE(TYPE) - passes if \a TYPE is fixed size.
  55. - \b EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(TYPE) - passes if \a TYPE is dynamic size.
  56. - \b EIGEN_STATIC_ASSERT_LVALUE(Derived) - failes if \a Derived is read-only.
  57. - \b EIGEN_STATIC_ASSERT_ARRAYXPR(Derived) - passes if \a Derived is an array expression.
  58. - <b>EIGEN_STATIC_ASSERT_SAME_XPR_KIND(Derived1, Derived2)</b> - failes if the two expressions are an array one and a matrix one.
  59. 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.
  60. \subsubsection StrictAssertions Strict assertions
  61. These assertions fail if the condition <b>may not</b> be met. For example, MatrixXd may not be a vector, so it fails EIGEN_STATIC_ASSERT_VECTOR_ONLY.
  62. - \b EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE) - passes if \a TYPE must be a vector type.
  63. - <b>EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(TYPE, SIZE)</b> - passes if \a TYPE must be a vector of the given size.
  64. - <b>EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(TYPE, ROWS, COLS)</b> - passes if \a TYPE must be a matrix with given rows and columns.
  65. \subsubsection PermissiveAssertions Permissive assertions
  66. These assertions fail if the condition \b cannot be met. For example, MatrixXd and Matrix4d may have the same size, so they pass EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE.
  67. - \b EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(TYPE0,TYPE1) - fails if the two vector expression types must have different sizes.
  68. - \b EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(TYPE0,TYPE1) - fails if the two matrix expression types must have different sizes.
  69. - \b EIGEN_STATIC_ASSERT_SIZE_1x1(TYPE) - fails if \a TYPE cannot be an 1x1 expression.
  70. See StaticAssert.h for details such as what messages they throw.
  71. \subsection DisableStaticAssert Disabling static assertions
  72. If \c EIGEN_NO_STATIC_ASSERT is defined, static assertions turn into <tt>eigen_assert</tt>'s, working like:
  73. \code
  74. #define EIGEN_STATIC_ASSERT(CONDITION,MSG) eigen_assert((CONDITION) && #MSG);
  75. \endcode
  76. This saves compile time but consumes more run time. \c EIGEN_NO_STATIC_ASSERT is undefined by default.
  77. */
  78. }