constexpr.cpp File Reference
#include "main.h"

Macros

#define EIGEN_TESTING_CONSTEXPR
 

Functions

 EIGEN_DECLARE_TEST (constexpr)
 

Macro Definition Documentation

◆ EIGEN_TESTING_CONSTEXPR

#define EIGEN_TESTING_CONSTEXPR

Function Documentation

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( constexpr  )
13  {
14  // Clang accepts (some of) this code when using C++14/C++17, but GCC does not like
15  // the fact that `T array[Size]` inside Eigen::internal::plain_array is not initialized
16  // until after the constructor returns:
17  // error: member ‘Eigen::internal::plain_array<int, 9, 0, 0>::array’ must be initialized by mem-initializer in
18  // ‘constexpr’ constructor
19 #if __cpp_constexpr >= 201907L
20  constexpr Matrix3i mat({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
21  VERIFY_IS_EQUAL(mat.size(), 9);
22  static_assert(mat(0, 0) == 1);
23  static_assert(mat(0) == 1);
24  static_assert(mat.coeff(0, 1) == 2);
25  constexpr Array33i arr({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
26  static_assert(arr(0, 0) == 1);
27  static_assert(arr(0) == 1);
28  VERIFY_IS_EQUAL(arr.size(), 9);
29  static_assert(arr.coeff(0, 1) == 2);
30  constexpr RowVector3i vec{{1, 2, 3}};
31  static_assert(vec(0, 0) == 1);
32  static_assert(vec[0] == 1);
33  VERIFY_IS_EQUAL(vec.size(), 3);
34  static_assert(vec.coeff(0, 1) == 2);
35 
36  // Also check dynamic size arrays/matrices with fixed-size storage (currently
37  // only works if all elements are initialized, since otherwise the compiler
38  // complains about uninitialized trailing elements.
39  constexpr Matrix<int, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> dyn_mat({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
40  VERIFY_IS_EQUAL(dyn_mat.size(), 9);
41  static_assert(dyn_mat(0, 0) == 1);
42  static_assert(dyn_mat.coeff(0, 1) == 2);
43  constexpr Array<int, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> dyn_arr({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
44  static_assert(dyn_arr(0, 0) == 1);
45  static_assert(dyn_arr(0) == 1);
46  VERIFY_IS_EQUAL(dyn_arr.size(), 9);
47  static_assert(dyn_arr.coeff(0, 1) == 2);
48 #endif // __cpp_constexpr >= 201907L
49 }
Eigen::SparseMatrix< double > mat
Definition: EigenUnitTest.cpp:10
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:48
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
Index size() const
Definition: SparseMatrixBase.h:187
Scalar coeff(Index row, Index col) const
Definition: SparseMatrix.h:211
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:367

References Eigen::SparseMatrix< Scalar_, Options_, StorageIndex_ >::coeff(), Eigen::SparseMatrixBase< Derived >::size(), and VERIFY_IS_EQUAL.