mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Make assignment constexpr
This commit is contained in:
committed by
Rasmus Munk Larsen
parent
4dda5b927a
commit
0ae7b59018
@@ -10,6 +10,13 @@
|
||||
#define EIGEN_TESTING_CONSTEXPR
|
||||
#include "main.h"
|
||||
|
||||
template <typename Scalar, int Rows>
|
||||
struct ConstexprTest {
|
||||
constexpr ConstexprTest(const Matrix<Scalar, Rows, Rows>& B) { A = B; }
|
||||
|
||||
Matrix<Scalar, Rows, Rows> A;
|
||||
};
|
||||
|
||||
EIGEN_DECLARE_TEST(constexpr) {
|
||||
// Clang accepts (some of) this code when using C++14/C++17, but GCC does not like
|
||||
// the fact that `T array[Size]` inside Eigen::internal::plain_array is not initialized
|
||||
@@ -33,6 +40,18 @@ EIGEN_DECLARE_TEST(constexpr) {
|
||||
VERIFY_IS_EQUAL(vec.size(), 3);
|
||||
static_assert(vec.coeff(0, 1) == 2);
|
||||
|
||||
// Check assignment. A wrapper struct is used to avoid copy ellision.
|
||||
constexpr ConstexprTest obj1(Matrix2d({{1, 2}, {3, 4}}));
|
||||
VERIFY_IS_EQUAL(obj1.A.size(), 4);
|
||||
static_assert(obj1.A(0, 0) == 1);
|
||||
static_assert(obj1.A(0) == 1);
|
||||
static_assert(obj1.A.coeff(0, 1) == 2);
|
||||
constexpr ConstexprTest obj2(Matrix3d({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}));
|
||||
VERIFY_IS_EQUAL(obj2.A.size(), 9);
|
||||
static_assert(obj2.A(0, 0) == 1);
|
||||
static_assert(obj2.A(0) == 1);
|
||||
static_assert(obj2.A.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.
|
||||
|
||||
Reference in New Issue
Block a user