当我使用警告标志运行以下命令时,我收到类型转换警告。
#include <boost/multi_array.hpp>
void function (boost::multi_array<unsigned char, 2> matrix) {
int nrows = matrix.shape()[0];
int ncols = matrix.shape()[1];
}
请参阅下面的警告消息。这是否意味着我隐式地将“long unsigned int”转换为常规“int”?
如果是这样,我想这就是我想要的(之后需要用nrows、ncols进行计算),那么我该如何使转换显式化呢?
image.cpp:93:32: warning: conversion to ‘int’ from ‘boost::const_multi_array_ref<float, 2ul, float*>::size_type {aka long unsigned int}’ may alter its value [-Wconversion]
int nrows = matrix.shape()[0];
这是否意味着我隐式地将“long unsigned int”转换为常规“int”?
是的,就是这个意思。
如果您不想要警告,则不要将
nrows
和 ncols
设为 int
类型。最简单的事情就是让编译器推断出类型,即
auto nrows = matrix.shape()[0];
auto ncols = matrix.shape()[1];
或者您可以将它们设为
size_t
类型,这是标准库用于容器大小的类型,并且不会发出警告。