[英]Is there an orthodox way to avoid compiler warning C4309 - “truncation of constant value” with binary file output?
[英]How to avoid warning message of “'=': truncation of constant value”?
在这里我主要是出于学术价值。 它对于8、16、32、64等非常灵活。在这种情况下,基础数组是32位指针。 每个单元包含32位,可以分别设置和清除。
Bitvector<unsigned long int> b(32);
我将每个32位单元的所有位都填充为1。
bool setAll()
{
for (int i = 0; i < m_size; i++)
{
if (m_intSize == 32)
m_array[i] = 0xFFFFFFFF;
else if (m_intSize == 64)
m_array[i] = 0xFFFFFFFFFFFFFFFF;
else if (m_intSize == 16)
m_array[i] = 0xFFFF;
else if (m_intSize == 8)
m_array[i] = 0xFF;
else
return false;
}
return true;
}
谁都可以猜到,一个32位动态数组指针不能指向64位,所以我在m_array [i] = 0xFFFFFFFFFFFFFFFFFF;上遇到了截断错误。
warning C4309: '=': truncation of constant value
有办法摆脱这种警告吗? 您是否可以执行某些选择,例如-1 * sizeof(T)或类似的选择?
是的,假定无符号值,通常的方法是:
void setAll()
{
for (int i = 0; i < m_size; i++)
m_array[i] = -1;
}
C ++中的无符号整数定义为可环绕 ,因此对于该平台上的m_array[i]
,这将始终为您提供最大可能的值。
或者,您可以#include <limits>
并使用std::numeric_limits<T>::max()
,但这并不那么酷。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.