为什么投不成功?

Why cast does not succeed?

我正在尝试这个:

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