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

时间:2021-08-31 19:17:05

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.

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

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

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

4 个解决方案

#1


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.

为什么要放在第一位?向量的大小通常是无符号整数。如果可能的话,我会说更新功能签名。警告并不意味着被压制,而是要解决。

#2


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这样的类。这将通过抛出异常来防止溢出或下溢的转换。

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

#3


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!

顺便说一下,你最后一次发布它就赞成了!

#4


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

由于size()通常返回无符号整数,因此将其类型化为签名整数应该是非常安全的。

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

Otherwise change the function signature, if it is possible.

否则,如果可能,请更改功能签名。

#1


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.

为什么要放在第一位?向量的大小通常是无符号整数。如果可能的话,我会说更新功能签名。警告并不意味着被压制,而是要解决。

#2


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这样的类。这将通过抛出异常来防止溢出或下溢的转换。

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

#3


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!

顺便说一下,你最后一次发布它就赞成了!

#4


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

由于size()通常返回无符号整数,因此将其类型化为签名整数应该是非常安全的。

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

Otherwise change the function signature, if it is possible.

否则,如果可能,请更改功能签名。