我正在尝试:
if constexpr (Bo == ByteOrder::Network && sizeof(T) == 8)
return reinterpret_cast<T &>
(be64toh(reinterpret_cast<make_unsigned_t<T>&>(val)));
else if constexpr (Bo == ByteOrder::Host && sizeof(T) == 8)
return reinterpret_cast<T &>(htobe64(reinterpret_cast<make_unsigned_t<T>&>(val)));
结果是:
error: invalid cast of an rvalue expression of type ‘__uint64_t {aka long unsigned int}’ to type ‘long int&’
return reinterpret_cast<T &>(be64toh(reinterpret_cast<make_unsigned_t<T>&>(val)));
最佳答案
这是一个疏忽:
只需转换为 T 而不是 T & 即可,因为该函数返回右值且不能绑定(bind)到左值。
关于c++ - 为什么投不成功?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54510036/