简体   繁体   English

std::bit_cast 和 std::start_lifetime_as 之间有什么有用的区别吗?

[英]Any useful difference between std::bit_cast and std::start_lifetime_as?

std::bit_cast is apparently being introduced in c++20. std::bit_cast显然是在 c++20 中引入的。 and std::start_lifetime_as is proposed for c++23 (from P0593R5 ).std::start_lifetime_as建议用于 c++23(来自P0593R5 )。 As they appear to both require that the datatypes involved be trivial anyways, will there be any need for the former once the latter is introduced?由于它们似乎都要求所涉及的数据类型无论如何都是微不足道的,因此一旦引入后者,是否需要前者?

Apologies in advance for not providing any more information about these new features.对于未提供有关这些新功能的更多信息,请提前致歉。 I only just heard about them after watching a cppcon 2019 lecture on type-punning, and I wasn't able to find much about start_lifetime_as with google.我只是在观看了关于类型双关语的 cppcon 2019 讲座后才听说它们,而且我在谷歌上找不到关于start_lifetime_as的太多信息。 I'm hoping someone else who sees this might know more.我希望看到这个的其他人可能会知道更多。

The answer is trivial: bit_cast returns a value, whereas start_lifetime_as “alters” memory (in a way that exists in the abstract machine but is not expected to affect any physical bits).答案很简单: bit_cast返回一个值,而start_lifetime_as “改变” memory(以抽象机器中存在但预计不会影响任何物理位的方式)。 You use the former to (once) interpret an existing object as a set of bits;您使用前者(一次)将现有的 object 解释为一组位; you use the latter to (permanently) interpret existing bits as an object.您使用后者(永久)将现有位解释为 object。

std::bit_cast copies the bits of its argument to a new value of a different type. std::bit_cast将其参数的位复制到不同类型的新值。

float myFloat = 3.14;
auto asUint = std::bit_cast<uint32_t>(myFloat);
auto asBytes = std::bit_cast<std::array<char,4>>(myFloat);

myFloat , asUint and asBytes are separate variables with separate addresses. myFloatasUintasBytes是具有不同地址的独立变量。 The compiler may be able to optimise some them away completely, but logically they are completely distinct values that just happen to have the same size and bits.编译器可能能够完全优化它们,但从逻辑上讲,它们是完全不同的值,恰好具有相同的大小和位。

std::start_lifetime_as doesn't do anything. std::start_lifetime_as不做任何事情。 It just informs the compiler that it can treat a range of memory as if it contained an array of the specified type.它只是通知编译器它可以将 memory 的范围视为包含指定类型的数组。 This then allows the developer use that memory as an array without triggering undefined behaviour.然后,这允许开发人员将 memory 用作数组,而不会触发未定义的行为。 It doesn't physically modify the memory passed to it and it doesn't return anything.它不会对传递给它的 memory 进行物理修改,也不会返回任何内容。 It's purely for C++ object model bookkeeping.它纯粹用于 C++ object model 簿记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 static_cast 之间有区别吗<unsigned> (有符号) vs std::bit_cast<unsigned> (签)? - Is there difference between static_cast<unsigned>(signed) vs std::bit_cast<unsigned>(signed)? 带有 std::array 的 std::bit_cast - std::bit_cast with std::array C++23 多线程应用程序中的 std::start_lifetime_as 和 UB - std::start_lifetime_as and UB in C++23 multithreaded application C++11 中 std::bit_cast 的安全等效项 - Safe equivalent of std::bit_cast in C++11 是否已经有一个 constexpr std::bit_cast 与 g++ 一起使用 - Is there already a constexpr std::bit_cast to use with g++ 通过 std::bit_cast()ed 指针对访问进行别名访问 - Aliasing accesses through a std::bit_cast()ed pointer 可以使用 std::bit_cast 从 std::span 转换<A>到 std::span<B>并像访问对象 B 一样访问吗?</a> - Can std::bit_cast be used to cast from std::span<A> to std::span<B> and access as if there was an array of object B? 文件 I/O 中的 std::bit_cast 与 reinterpret_cast - std::bit_cast vs reinterpret_cast in file I/O std::bit_cast 生成多个值的值表示的示例是什么? - What would be an example where std::bit_cast produces a value representation of multiple values? C++20 特性 std::bit_cast:重新解释类型 from 到 type to 时值会发生什么 - C++20 feature std::bit_cast : what happens to the value while reinterpreting type from to type to
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM