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

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

问题描述

我需要一个通用的 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 做很多操作:保存、加法、乘法、循环等.重要:但是如果 std::_Array_iterator<_Ty,_Size>::difference_typeptrdiff_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>::difference_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_typeptrdiff_t 不同会怎样种类?"不要使用这样的编译器.此外,很可能它不能在形式上有所不同.例如.这是使用默认标准分配器的 vector 的情况,因为这是它获取其 typedef 的地方,但由于正式保证并不重要(嘿嘿,真的不重要)我不会在 C++0x 草案中查找这个.

"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 自然伴随着自定义的独立 countOfstartOfendOf 函数,使您能够处理原始数组和标准库容器完全一样.

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 时,就更清楚它是一个索引,不能很自然地脱离IndexSize 类型集几乎不管你做什么.例如,给它添加一些东西,它仍然是一个 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".

但在极少数情况下,您需要从 Index 变为 int,例如.在极少数情况下,只需执行 static_cast 即可关闭编译器并明确您的意图.或者甚至是一个自定义的 static_cast-like narrowTo 操作,为了表现力...

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...

干杯&第八,

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

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