blob: b8f0b09204c3f80f722436d02af104b73e0b2f2f [file] [log] [blame]
Alexander Richardson62de5932022-12-14 17:05:37 +00001// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2022 Alex Richardson <alexrichardson@google.com>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#include "main.h"
11
12EIGEN_DECLARE_TEST(constexpr) {
13 // Clang accepts (some of) this code when using C++14/C++17, but GCC does not like
14 // the fact that `T array[Size]` inside Eigen::internal::plain_array is not initialized
15 // until after the constructor returns:
16 // error: member ‘Eigen::internal::plain_array<int, 9, 0, 0>::array’ must be initialized by mem-initializer in
17 // ‘constexpr’ constructor
Antonio Sánchezdbf7ae62022-12-20 18:06:03 +000018#if EIGEN_COMP_CXXVER >= 20
Alexander Richardson62de5932022-12-14 17:05:37 +000019 constexpr Matrix3i mat({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
20 VERIFY_IS_EQUAL(mat.size(), 9);
21 VERIFY_IS_EQUAL(mat(0, 0), 1);
22 static_assert(mat.coeff(0,1) == 2);
23 constexpr Array33i arr({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
24 VERIFY_IS_EQUAL(arr(0, 0), 1);
25 VERIFY_IS_EQUAL(arr.size(), 9);
26 static_assert(arr.coeff(0,1) == 2);
27 // Also check dynamic size arrays/matrices with fixed-size storage (currently
28 // only works if all elements are initialized, since otherwise the compiler
29 // complains about uninitialized trailing elements.
30 constexpr Matrix<int, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> dyn_mat({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
31 VERIFY_IS_EQUAL(dyn_mat.size(), 9);
32 VERIFY_IS_EQUAL(dyn_mat(0, 0), 1);
33 static_assert(dyn_mat.coeff(0,1) == 2);
34 constexpr Array<int, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> dyn_arr({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
35 VERIFY_IS_EQUAL(dyn_arr(0, 0), 1);
36 VERIFY_IS_EQUAL(dyn_arr.size(), 9);
37 static_assert(dyn_arr.coeff(0,1) == 2);
Antonio Sánchezdbf7ae62022-12-20 18:06:03 +000038#endif // EIGEN_COMP_CXXVER >= 20
Alexander Richardson62de5932022-12-14 17:05:37 +000039}
40
41// Check that we can use the std::initializer_list constructor for constexpr variables.
42#if EIGEN_COMP_CXXVER >= 20
43// EIGEN_MAKE_UNALIGNED_ARRAY_ASSERT() will fail constexpr evaluation unless
44// we have std::is_constant_evaluated().
45constexpr Matrix<int, 2, 2> global_mat({{1, 2}, {3, 4}});
46
47EIGEN_DECLARE_TEST(constexpr_global) {
48 VERIFY_IS_EQUAL(global_mat.size(), 4);
49 VERIFY_IS_EQUAL(global_mat(0, 0), 1);
50 static_assert(global_mat.coeff(0,0) == 1);
51}
52#endif // EIGEN_COMP_CXXVER >= 20