inline void addHeader(T value)
{
if(sizeof(T) > m_outputBufferStart)
{
std::clog << "[Error - OutputMessage::addHeader] m_outputBufferStart(" << m_outputBufferStart << ") < " << sizeof(T) << std::endl;
return;
}
m_outputBufferStart -= sizeof(T);
*(T*)(m_buffer + m_outputBufferStart) = value; //here is error
m_size += sizeof(T); //current size of buffer
}
我的问题是如何解决严格的别名问题?我不想&#34;跳过&#34;这个警告,
buffer是uint8_t
的数组答案 0 :(得分:1)
通过将数组值转换为整数来进行指针运算,可以使警告静音。
*(T*)((uintptr_t)m_buffer + m_outputBufferStart) = value;
更多C ++方式可能是将类型转换为指针然后将其复制到缓冲区。
std::copy_n(&m_buffer[m_outputBufferStart], sizeof(T), (uint8_t*)value);