我应该使用什么类型的迭代器差异来消除“可能的数据丢失”警告? [英] What type should I use for iterator difference to eliminate "possible loss of data" warnings?

查看:108
本文介绍了我应该使用什么类型的迭代器差异来消除“可能的数据丢失”警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need a common rule for warnings in x64 mode. Which way is better?

请考虑以下几行代码

const int N = std::max_element(cont.begin(), cont.end()) - cont.begin();

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

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

It is my usual code. I have no problems with x86.

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

But if I run compiler in x64 mode I have some warnings:

conversion from 'std::_Array_iterator<_Ty,_Size>::difference_type' to 'int', possible loss of data
conversion from '__int64' to 'int', possible loss of data

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

I want to solve these problems by common rule. Which way is better?


  1. 制作 static_cast

const int N = static_cast<int>(
     std::max_element(cont.begin(), cont.end()) - cont.begin()  );

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

I think this is not general-purpose. And too much letters.

使用 ptrdiff_t 替换输出类型:

const ptrdiff_t N = std::max_element(cont.begin(), cont.end()) - cont.begin();

那么对于这个未知类型ptrdiff_t应该怎么办?
然后我会得到另外十几个警告。
我想用N:save,addition,multiplication,cycles等方法进行许多操作。
重要:但是如果 std :: _ Array_iterator< _Ty,_Size> :: difference_type ptrdiff_t 是不同类型吗?

What should I do then with this unknown type ptrdiff_t? I'll get another dozen warnings then. I want to make many operations with N: save, addition, multiplication, cycles and etc. Important: but what if std::_Array_iterator<_Ty,_Size>::difference_type and ptrdiff_t are different types?

std :: _ Array_iterator< _Ty,_Size> :: replace_type :替换输出类型:

//h-file
struct S {
    type mymember; // What is the type?
};


//cpp-file
typedef std::vector<int> cont_t;
const cont_t::difference_type N = std::max_element(cont.begin(), cont.end()) - cont.begin();
// Save N
S mystruct;
mystruct.mymember = N; // What type of mystruct.mymember?

如何保存N?什么类型的mystruct.mymember?我不知道它在h档。

How should I save N? What type of mystruct.mymember? I don't know it in h-file.

您的解决方案。


推荐答案

如果 std :: _ Array_iterator <_Ty,_Size> :: difference_type c $ c> ptrdiff_t 是不同类型?不要使用这样的编译器。此外,它可能不能在形式上不同。例如。这是一个向量使用默认标准分配器的情况,因为这是它获取它的typedefs,但由于形式保证无关紧要(他,他真的没有

"what if std::_Array_iterator<_Ty,_Size>::difference_type and ptrdiff_t are different types?" Don't use such a compiler. Also, chances are that it can't formally be different. E.g. this is the case for a vector using the default standard allocator, since that's where it fetches its typedefs, but since the formal guarantee doesn't matter (he he, it really doesn't) I'm not going to look this up in the C++0x draft.

所以,使用 ptrdiff_t

但是添加一些typedef可能是一个好主意,例如

But it can be a good idea to add a few typedefs, like

typedef ptrdiff_t Size;
typedef ptrdiff_t Index;

然后在具体的情况下,你会使用 Index

and then in your concrete case you'd use Index.

这些typedef自然伴随着自定义独立 countOf startOf endOf 函数,使您能够以完全相同的方式处理原始数组和标准库容器。

These typedefs are naturally accompanied by custom freestanding countOf, startOf and endOf functions, enabling you to treat raw arrays and standard library containers in exactly the same way.

当你看到 Index 这个名称时,它就更清楚了,它是一个索引,它不能很自然地从索引大小几乎无论你做什么类型的类型。例如,添加一些东西,它仍然是索引

When you see the name Index it's a bit more clear that it's an index, which can't very naturally get out of the Index or Size set of types almost no matter what you do. E.g., add something to it, it's still an Index. So mostly there will not be a "another dozen warnings".

但在少数情况下,你需要从索引 int ,说。在这种罕见的情况下,只需做一个 static_cast 来关闭编译器,使你的意图清楚。或者甚至自定义 static_cast -like 操作,以表现力...

But in some rare case you'll need to get from Index to just int, say. In and in those rare cases just do a static_cast to shut up the compiler and make your intent clear. Or even a custom static_cast-like narrowTo operation, for expressiveness...

干杯hth。,

这篇关于我应该使用什么类型的迭代器差异来消除“可能的数据丢失”警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆