为什么投射不成功

Why cast does not succeed?

本文关键字:不成功 为什么      更新时间:2023-10-16

我正在尝试这个:

 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 即可工作,因为该函数返回右值并且不能绑定到左值。