Skip to content

IndexedView of a vector should allow linear access

At least I think that's what the compiler error means. Can be worked-around by an extra call to reshaped.

/opt/compiler-explorer/libs/eigen/vtrunk/Eigen/src/Core/DenseCoeffsBase.h:142:7: error: static assertion failed: THIS_COEFFICIENT_ACCESSOR_TAKING_ONE_ACCESS_IS_ONLY_FOR_EXPRESSIONS_ALLOWING_LINEAR_ACCESS

  142 |       EIGEN_STATIC_ASSERT(internal::evaluator<Derived>::Flags & LinearAccessBit,

      |       ^~~~~~~~~~~~~~~~~~~
#include <iostream>
#include <Eigen/Dense>
int main(){

    auto a = Eigen::VectorXi(10);
    auto b = Eigen::VectorXi(5);
    auto c = Eigen::VectorXi(3);

    a << 10,11,12,13,14,15,16,17,18,19;
    b << 0, 1, 2, 3, 4;
    c << 3, 2, 2;

    std::cout << a << std::endl;
    std::cout << b << std::endl;
    std::cout << c << std::endl;

    std::cout << a(b(c)) << std::endl; // does not work: b(c) does not allow linear access

    std::cout << a(b(c).reshaped()) << std::endl; // works

}

https://gcc.godbolt.org/z/7vHSuZ