从“xxxx”转换为 到“yyyy”,可能会丢失数据,抑制?

发布于 2024-07-16 21:44:47 字数 732 浏览 4 评论 0原文

有时我会收到从较长类型转换为较小类型的警告,例如:

void f( unsigned short i ) // f - accept any numeric type
                           // smaller than std::vector<>::size_type
{}

std::vector < some_type > v;
..
f ( v.size() );

通常我正在使用下一个解决方案之一:

assert( v.size() <= std::numeric_limits< unsigned short >::max() );
f( static_cast< unsigned short >( v.size() ) );

f( boost::numeric_cast<  unsigned short >( v.size() ) );

但在我目前的工作中未使用升压,并且从上个月开始不允许断言。

您还知道哪些其他安全方法可以抑制此警告?
所描述的方式有什么陷阱吗?

PS: 并不总是可以更改 f 的签名,有时也确实应该接受小数字类型。

编辑: 我想让转换尽可能安全。

Sometimes I've got warnings with conversion from a longer type to a smaller type e.g.:

void f( unsigned short i ) // f - accept any numeric type
                           // smaller than std::vector<>::size_type
{}

std::vector < some_type > v;
..
f ( v.size() );

Usually I was using one of next solutions:

assert( v.size() <= std::numeric_limits< unsigned short >::max() );
f( static_cast< unsigned short >( v.size() ) );

or

f( boost::numeric_cast<  unsigned short >( v.size() ) );

But on my present work boost not used and from last month asserts are disallowed.

What other safe ways you know for suppress this warning?
Any pitfalls in discribed ways?

PS:
It is not always possible to change the signature of f, also sometimes really should accept small numeric type.

EDITED:
I want to make conversion as safe as possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白龙吟 2024-07-23 21:44:48

处理此问题的唯一安全方法是确保运行时不会丢失转换。 断言代码仅在调试构建期间起作用,并且允许零售构建中的转换损失。 转换损失很糟糕,因为它将传递完全不正确的向量大小。

您真正需要的是一种防止数据丢失的机制。 我推荐使用像 SafeInt 这样的类。 这将防止通过抛出异常而导致上溢或下溢的转换。

SafeInt<size_t> size = v.size();
f((unsigned short)size);  // Throws if size can't fit in an unsigned short

SafeInt:http://www.codeplex.com/SafeInt

The only safe way to deal with this is to ensure that you do not have a loss of conversion at runtime. The assert code will only work during debug builds and will allow for a conversion loss in retail builds. The conversion loss is bad because it will pass around a completely incorrect size for the vector.

What you really need is a mechanism to prevent you from creating data loss. I reccomend using a class like SafeInt. This will prevent a conversion which overflows or underflows by means of throwing an exception.

SafeInt<size_t> size = v.size();
f((unsigned short)size);  // Throws if size can't fit in an unsigned short

SafeInt: http://www.codeplex.com/SafeInt

爱人如己 2024-07-23 21:44:48

现在我将再次重复我的口头禅:如果您的代码包含强制转换,则代码或设计可能存在问题,您应该检查两者以删除强制转换。

顺便说一句,我上次发布时你已经点赞了!

I will now repeat my mantra again: If your code contains casts, there is probably something wrong with the code or the design and you should examine both with a view to removing the cast.

BTW, you upvoted this the last time I posted it!

葬シ愛 2024-07-23 21:44:48

由于 size() 通常返回一个无符号整数,因此将其类型转换为有符号整数应该是非常安全的。

f(static_cast<expected-type>(v.size()));

否则,如果可能的话,更改函数签名。

As size() usually returns an unsigned integer, it should be quite safe to typecast it to a signed one.

f(static_cast<expected-type>(v.size()));

Otherwise change the function signature, if it is possible.

注定孤独终老 2024-07-23 21:44:47

为什么首先要投? 向量的大小通常是无符号整数。 如果可能的话,我会说更新函数签名。 警告并不是要被抑制,而是要被解决。

Why cast in the first place? The vector's size is typically an unsigned integer. If possible, I'd say update the function signature. Warnings are not meant to be suppressed, rather addressed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文