c++ - 为什么投不成功?

标签 c++ linux endianness

我正在尝试:

 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/

相关文章:

C++如何读取流直到行尾

c++ - 将嵌套表从 Lua 传递到 C

c++ - 强制 Eclipse SVN 检查 Eclipse 外部更改的文件

linux - node.js 和 serialport 无法列出任何端口

.net - 防止 IO.GetDirectories() 遵循符号链接(symbolic link)

php - 从 Symfony 命令运行 Linux 命令

c - fread 似乎是从右到左读取文件

c++ - 如何在自己的类中使用 wxWidgets 类型?

python - 如何在 python 中将 Integer 转换为 64 位大端整数

将十六进制数组 (char*) 转换为整数