【问题标题】:Accessing dimension of boost multi-arrays in C++在 C++ 中访问 boost 多数组的维度
【发布时间】:2021-12-31 04:02:56
【问题描述】:

当我使用警告标志运行以下命令时,我会收到类型转换警告。

#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];

【问题讨论】:

    标签: c++ boost


    【解决方案1】:

    这是否意味着我正在将“long unsigned int”隐式转换为常规“int”?

    是的,就是这个意思。

    如果您不想收到警告,请不要将 nrowsncols 设为 int 类型。最简单的方法是让编译器推断类型,即

    auto nrows = matrix.shape()[0];
    auto ncols = matrix.shape()[1];
    

    或者您可以将它们设为size_t 类型,这是标准库用于确定容器大小并且不会发出警告的类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-01
      • 1970-01-01
      • 2012-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多