c++ 使用某些模板化方法理解reinterpret_cast错误

osh3o9ms  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(176)

我正在尝试用一些c++ ARM编译器编译下面的代码。

void pairs_converter(const mem_t& mem, uint16_t* data) {/* some stuff */}

template <typename StringT>
inline 
void to_string(const mem_t& mem, StringT& s)
{
    s.resize(24);
    pairs_converter(mem, reinterpret_cast<uint16_t*>(&s[0]));
}

字符串
我得到了以下错误代码:

error: cast from '__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type*' {aka 'char*'} to 'uint16_t*' {aka 'short unsigned int*'} increases required alignment of target type [-Werror=cast-align]
         pairs_converter(mem, reinterpret_cast<uint16_t*>(&s[0]));


据我所知,它看起来像是一些单词和八位字节之间的对齐问题,但我对c不够流利,无法理解如何解决这个编译器错误。
我会很感激一些更详细的解释这个c
错误!

x6h2sr28

x6h2sr281#

我的一般建议是不要使用reinterpret_cast,除非你确切地知道你在做什么。如果你真的需要它,你应该在文档中真正熟悉它。

相关问题