Skip to content

Static asserts to check for matching NumDimensions

What does this implement/fix?

The two Tensor constructors:

    template<typename OtherDerived>
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE Tensor(const TensorBase<OtherDerived, ReadOnlyAccessors>& other)

...

    template<typename OtherDerived>
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE Tensor(const TensorBase<OtherDerived, WriteAccessors>& other)

do not currently make any checks about the dimensions of OtherDerived. This means that code such as:

void a_function(Eigen::Tensor<float, 2> const &a);

int main() {
Eigen::Tensor<float, 3> b;

a_function(b);
}

compiles - see: https://godbolt.org/z/TT71bxzbh. However, this code will almost certainly crash at runtime due to mismatched dimensions.

This MR adds EIGEN_STATIC_ASSERT() to both constructors to check the number of dimensions.

I am not sure if operator= in Tensor.h should have the same checks. In my own codebase, adding the above was sufficient to catch dimension errors even in assignment expressions.

Merge request reports

Loading