Is this declaration of an Eigen::Tensor in C++ safe, or buggy? And should I submit an issue for it?

非 Y 不嫁゛ 提交于 2019-12-23 22:26:49

问题


Using Eigen's unsupported Tensor module, if I do:

    size_t dim0 = 3;
    size_t dim1 = 2;
    size_t dim2 = 4;
    Eigen::Tensor<double, 3> var(dim0, dim1, dim2);

I get the following error:

/usr/local/include/eigen3/unsupported/Eigen/CXX11/src/Tensor/TensorDimensions.h:287:167: error: non-constant-expression cannot be narrowed from type 'unsigned long' to 'std::__1::array<long, 3>::value_type' (aka 'long') in initializer list [-Wc++11-narrowing]

But the code compiles OK if I explicitly cast the dimensions to long int:

    long int dim0 = 3;
    long int dim1 = 2;
    long int dim2 = 4;
    Eigen::Tensor<double, 3> var(dim0, dim1, dim2);

Questions:

  1. For what size variable will this become unsafe? If at all?
  2. Surely Eigen should be generally accepting a (size_t) type as a dimension argument? Should I file a bug report for this or is it intended behaviour here?

I'm using C++11, clang on Mac OSX (haven't tested other platforms).


回答1:


The narrowing warning will appear for any type that cannot be converted to a long without loss. So that means size_t, but also long long on some platforms.

In general, loop indices should be signed, hence Eigen decision to store long for sizes.

For more info on this, there are some cppcon topics (Chandler Carruth, mainly) about undefined behavior that help the compiler.



来源:https://stackoverflow.com/questions/54233187/is-this-declaration-of-an-eigentensor-in-c-safe-or-buggy-and-should-i-subm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!