问题描述
如果我使用Eigen不受支持的Tensor模块,则:
size_t dim0 = 3;
size_t dim1 = 2;
size_t dim2 = 4;
Eigen :: Tensor< double,3> var(dim0,dim1,dim2);
我收到以下错误:
/usr/local/include/eigen3/unsupported/Eigen/CXX11/src/Tensor/TensorDimensions.h:287:167:错误:非常量表达式无法从'unsigned类型缩小long到初始值设定项列表[-Wc ++ 11-narrowing]
但是,如果我将尺寸显式转换为long int,则代码可以编译OK:
long int dim0 = 3;
long int dim1 = 2;
long int dim2 = 4;
Eigen :: Tensor< double,3> var(dim0,dim1,dim2);
问题:
- 对于什么大小的变量,这将变得不安全?
- Eigen当然应该普遍接受
(size_t)
类型作为维度参数吗?我应该为此提交错误报告还是在这里执行预期的行为?
我在Mac上使用C ++ 11 OSX(尚未测试其他平台)。
解决方案对于任何不能转换为
长
而不会丢失。因此,这意味着size_t
,但是在某些平台上也意味着long long
。
通常,应该对循环索引进行签名,因此Eigen决定存储
long
个大小。
有关此的更多信息,有一些cppcon主题(主要是Chandler Carruth),涉及未定义的行为,可帮助编译器。
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:
- For what size variable will this become unsafe? If at all?
- 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).
解决方案The narrowing warning will appear for any type that cannot be converted to a
long
without loss. So that meanssize_t
, but alsolong 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.
这篇关于使用C ++编写的Eigen :: Tensor声明是安全的还是越野车?我应该为此提交问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!