- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在从网络接收缓冲区,该缓冲区已转换为 32 位字数组。我有一个词被我的接口(interface)文档定义为 IEEE-754 float 。我需要从缓冲区中提取这个词。在不调用转换的情况下很难从一种类型转换为另一种类型。这些位已经符合 IEEE-754 浮点标准,我不想重新安排任何位。
我的第一个尝试是将 uint32_t
的地址转换为 void*
,然后将 void*
转换为 float*
,然后解引用为 float
:
float ieee_float(uint32_t f)
{
return *((float*)((void*)(&f)));
}
error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
我的第二次尝试是这样的:
float ieee_float(uint32_t f)
{
union int_float{
uint32_t i;
float f;
} tofloat;
tofloat.i = f;
return tofloat.f;
}
然而,街上的消息是 union 是完全不安全的。从不是最近写入的 union 成员中读取是未定义的行为。
所以我尝试了一种更 C++ 的方法:
float ieee_float(uint32_t f)
{
return *reinterpret_cast<float*>(&f);
}
error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
我的下一个想法是“去他的。我为什么还要处理指针?”刚刚试过:
float ieee_float(uint32_t f)
{
return reinterpret_cast<float>(f);
}
error: invalid cast from type ‘uint32_t {aka unsigned int}’ to type ‘float’
有没有办法在不触发警告/错误的情况下进行转换?我正在使用 -Wall -Werror
使用 g++ 进行编译。我不想触及编译器设置。
我标记 C 是因为 c 解是可以接受的。
最佳答案
在 C++20 中,您可以使用 std::bit_cast
:
float ieee_float(uint32_t f)
{
return std::bit_cast<float>(f);
}
在 C++17 及之前的版本中,正确的方法™ 是:
float ieee_float(uint32_t f)
{
static_assert(sizeof(float) == sizeof f, "`float` has a weird size.");
float ret;
std::memcpy(&ret, &f, sizeof(float));
return ret;
}
GCC 和 Clang 在 -O1
及以上代码为此代码和天真的 reinterpret_cast<float &>(f)
生成相同的程序集(但后者是未定义的行为,在某些情况下可能不起作用)。
关于c++ - 按位将 uint32_t 强制转换为 C/C++ 中的 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48803363/
根据在线文档,这些固定宽度整数类型之间存在差异。对于 int*_t,我们将宽度固定为 * 的值。然而对于其他两种类型,描述中使用形容词最快和最小来请求底层数据模型提供的最快或最小实例。 “最快”或“最
我是 Python 注释的新手(类型提示)。我注意到 pyi 中的许多类定义文件继承到 Generic[_T] , 和 _T = TypeVar('_T') . 我很困惑,_T 是什么意思?这里的意思
这个问题在这里已经有了答案: How to use a variable inside a _T wrapper? (3 个答案) 关闭 7 年前。 我有以下代码: CString port = m
要包含 _T() 宏,我应该包含什么文件?它转换我认为的文本文字。我以为它是 windows.h,但我已经包含了它。 令人惊讶的是,我无法在 Google 上找到答案。 最佳答案 我在主题 Unico
类型的后缀 _t 由 POSIX 保留,但是如果我在自己的命名空间中使用 _t 后缀定义自己的类型怎么办? 最佳答案 我同意 user6366161 的 answer,其中说“C 对 namespac
我知道 size_t 有 _t 后缀,因为它的别名/typedef。但是我不明白为什么 char16_t, char32_t 和 wchar_t 包含 _t 后缀。 最佳答案 对于 wchar_t :
我想让这个字符串的主机名部分可变..目前,它只修复了这个 URL: _T(" --url=http://www.myurl.com/ --out=c:\\current.png"); 我想做这样的东西
这个问题在这里已经有了答案: convert string to _T in cpp (6 个答案) 关闭 7 年前。 string pagexx = "http://website.com/" +
我有一个注册为 COM 对象的 .net 库,当在 C++ 项目中导入 .tlb 文件时,我得到这样的方法声明 virtual HRESULT __stdcall GetBid ( /*[
我现在遇到了很多 Unicode 问题。据我了解,TCHAR 被定义为 wchar_t 或 char,具体取决于 _UNICODE 是否在某处定义,并且还有各种其他功能可以帮助解决这个问题。显然 _T
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve t
我正在尝试使用 _t() 方法翻译一个 DataObject。 我一直在 Pages 上使用它没有问题,但它似乎不适用于数据对象。 class SliderItem extends DataObjec
关于保留 _t 结尾名称的规则是否也适用于作用域名称(例如,在 namespace 或类中定义的类型和类型定义),还是仅适用于全局 namespace 中的类型和类型定义?标准 C/C++ 库或 PO
我确定以前有人问过这个问题,但我无法搜索到文本。如果有人可以解释它们,请给我推荐一篇文章,或者给我正确的搜索查询,我将不胜感激。 谢谢。 最佳答案 这只是一种预感,但看看 Wikipedia C++1
我有一个 UNICODE 应用程序,我们使用 _T(x) 定义如下。 #if defined(_UNICODE) #define _T(x) L ##x #else #define _T(x) x #
我一直想知道是否存在任何命名约定,例如何时对类型使用 ALLCAPS 以及何时附加 _t(以及何时不使用任何东西?)。我知道以前 K&R 发布了各种关于如何使用 C 的文档,但我找不到任何相关内容。
这似乎是一个简单的问题,但我无法通过 Stack Overflow 搜索或 Google 找到它。类型后跟 _t 是什么意思?比如 int_t anInt; 我在 C 代码中经常看到它与硬件密切相关—
C++ 有时使用后缀 _type关于类型定义(例如 std::vector::value_type ),有时_t (例如 std::size_t ),或者没有后缀(普通类,还有像 std::strin
字符串中的“T”代表什么。例如 _T("Hello")。我在需要 unicode 支持的项目中看到了这一点。它实际上告诉处理器什么 最佳答案 _T 代表“文本”。当且仅当您使用 Unicode 支持编
我的代码可以根据 C++ 类型识别您需要使用的 GL 类型。我想制作它的 _t 版本(如 std::decay_t 或 std::enable_if_t)但公开 int常量值 template st
我是一名优秀的程序员,十分优秀!