The `data` method could not be used in a `constexpr`context

Summary

The method template<class Derived> inline const Eigen::MapBase<Derived, 0>::Scalar *Eigen::MapBase<Derived, 0>::data() const in the Eigen library is not marked as constexpr, even when the EIGEN_CONSTEXPR macro is enabled. This prevents the data method from being used in constant expressions. Consequently, code that relies on constexpr evaluation, such as the example provided, fails to compile.

Environment

  • Operating System: Linux
  • Architecture: x64
  • Eigen Version: ALL
  • Compiler Version: Gcc 12.x
  • Compile Flags: -O3 -march=native -std=c++17
  • Vector Extension: SSE/AVX/NEON

Minimal Example

#include <Eigen/Dense>
#include <algorithm>

constexpr auto my_func(const Eigen::Ref<const Eigen::VectorXd>& arr) -> int {
  int i = 0;
  std::for_each(arr.data(), arr.data() + arr.size(), [&i](const double x) { ++i; });
  return i;
}

Steps to reproduce

  1. Include Eigen headers and necessary standard library headers.
  2. Define a constexpr function that attempts to use Eigen::Ref<const Eigen::VectorXd>::data() within a constexpr context.
  3. Compile the code with a C++17 or later compliant compiler.

What is the current bug behavior?

The code does not compile because the data method of Eigen::Ref is not constexpr. This results in a compilation error indicating that the data method cannot be used in a constant expression.

What is the expected correct behavior?

The code should compile successfully, allowing the data method to be used in a constexpr context. The data method should be marked as constexpr to support such usage.

Relevant logs

The following is an example of the compilation error message:

error: call to non-constexpr function ‘const Scalar* Eigen::MapBase<Derived, 0>::data() const’
   std::for_each(arr.data(), arr.data() + arr.size(), [&i](const double x) { ++i; });

Warning Messages

No specific warning messages, just a compilation error.

Benchmark scripts and results

N/A

Anything else that might help

The issue could be addressed by modifying the Eigen library to ensure that the data method in the MapBase class is marked as constexpr when EIGEN_CONSTEXPR is enabled. This change would enable the data method to be used in constant expressions, ensuring compatibility with modern C++ features and idioms.

  • Have a plan to fix this issue.