相关疑难解决方法(0)

我应该使用什么类型的迭代器差异来消除“可能丢失数据”警告?

我需要 x64 模式下警告的通用规则。哪种方式更好?

考虑以下几行代码

const int N = std::max_element(cont.begin(), cont.end()) - cont.begin();
Run Code Online (Sandbox Code Playgroud)

或者

const int ARR_SIZE = 1024;
char arr[ARR_SIZE];
//...
const int N = std::max_element(arr, arr + ARR_SIZE) - arr;
Run Code Online (Sandbox Code Playgroud)

这是我常用的代码。我用 x86 没有任何问题。

但如果我在 x64 模式下运行编译器,我会收到一些警告:

conversion from 'std::_Array_iterator<_Ty,_Size>::difference_type' to 'int', possible loss of data
conversion from '__int64' to 'int', possible loss of data
Run Code Online (Sandbox Code Playgroud)

我想通过共同规则来解决这些问题。哪种方式更好?

  1. 制作static_cast

    const int N = static_cast<int>(
         std::max_element(cont.begin(), cont.end()) - cont.begin()  );
    
    Run Code Online (Sandbox Code Playgroud)

    我认为这不是通用的。而且字母太多。

  2. 将输出类型替换为ptrdiff_t

    const ptrdiff_t N = std::max_element(cont.begin(), cont.end()) - …
    Run Code Online (Sandbox Code Playgroud)

c++ 64-bit x86 warnings visual-studio-2010

5
推荐指数
1
解决办法
3829
查看次数

标签 统计

64-bit ×1

c++ ×1

visual-studio-2010 ×1

warnings ×1

x86 ×1