mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Make element accessors constexpr
This commit is contained in:
committed by
Charles Schlosser
parent
8b4efc8ed8
commit
0fb2ed140d
@@ -16,38 +16,48 @@ EIGEN_DECLARE_TEST(constexpr) {
|
||||
// until after the constructor returns:
|
||||
// error: member ‘Eigen::internal::plain_array<int, 9, 0, 0>::array’ must be initialized by mem-initializer in
|
||||
// ‘constexpr’ constructor
|
||||
#if EIGEN_COMP_CXXVER >= 20
|
||||
#if __cpp_constexpr >= 201907L
|
||||
constexpr Matrix3i mat({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
|
||||
VERIFY_IS_EQUAL(mat.size(), 9);
|
||||
VERIFY_IS_EQUAL(mat(0, 0), 1);
|
||||
static_assert(mat(0, 0) == 1);
|
||||
static_assert(mat(0) == 1);
|
||||
static_assert(mat.coeff(0, 1) == 2);
|
||||
constexpr Array33i arr({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
|
||||
VERIFY_IS_EQUAL(arr(0, 0), 1);
|
||||
static_assert(arr(0, 0) == 1);
|
||||
static_assert(arr(0) == 1);
|
||||
VERIFY_IS_EQUAL(arr.size(), 9);
|
||||
static_assert(arr.coeff(0, 1) == 2);
|
||||
constexpr RowVector3i vec{{1, 2, 3}};
|
||||
static_assert(vec(0, 0) == 1);
|
||||
static_assert(vec[0] == 1);
|
||||
VERIFY_IS_EQUAL(vec.size(), 3);
|
||||
static_assert(vec.coeff(0, 1) == 2);
|
||||
|
||||
// Also check dynamic size arrays/matrices with fixed-size storage (currently
|
||||
// only works if all elements are initialized, since otherwise the compiler
|
||||
// complains about uninitialized trailing elements.
|
||||
constexpr Matrix<int, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> dyn_mat({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
|
||||
VERIFY_IS_EQUAL(dyn_mat.size(), 9);
|
||||
VERIFY_IS_EQUAL(dyn_mat(0, 0), 1);
|
||||
static_assert(dyn_mat(0, 0) == 1);
|
||||
static_assert(dyn_mat.coeff(0, 1) == 2);
|
||||
constexpr Array<int, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> dyn_arr({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
|
||||
VERIFY_IS_EQUAL(dyn_arr(0, 0), 1);
|
||||
static_assert(dyn_arr(0, 0) == 1);
|
||||
static_assert(dyn_arr(0) == 1);
|
||||
VERIFY_IS_EQUAL(dyn_arr.size(), 9);
|
||||
static_assert(dyn_arr.coeff(0, 1) == 2);
|
||||
#endif // EIGEN_COMP_CXXVER >= 20
|
||||
#endif // __cpp_constexpr >= 201907L
|
||||
}
|
||||
|
||||
// Check that we can use the std::initializer_list constructor for constexpr variables.
|
||||
#if EIGEN_COMP_CXXVER >= 20
|
||||
#if __cpp_constexpr >= 201907L
|
||||
// EIGEN_MAKE_UNALIGNED_ARRAY_ASSERT() will fail constexpr evaluation unless
|
||||
// we have std::is_constant_evaluated().
|
||||
constexpr Matrix<int, 2, 2> global_mat({{1, 2}, {3, 4}});
|
||||
|
||||
EIGEN_DECLARE_TEST(constexpr_global) {
|
||||
VERIFY_IS_EQUAL(global_mat.size(), 4);
|
||||
VERIFY_IS_EQUAL(global_mat(0, 0), 1);
|
||||
static_assert(global_mat(0, 0) == 1);
|
||||
static_assert(global_mat(0) == 1);
|
||||
static_assert(global_mat.coeff(0, 0) == 1);
|
||||
}
|
||||
#endif // EIGEN_COMP_CXXVER >= 20
|
||||
#endif // __cpp_constexpr >= 201907L
|
||||
|
||||
Reference in New Issue
Block a user