最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Why does bit cast require both types to be trivially-copyable? - Stack Overflow

programmeradmin5浏览0评论

Why does C++ std::bit_cast require both To and From to be trivially-copyable? For example:

From from{};
To to;
static_assert(sizeof to == sizeof from);
std::memcpy(&to, &from, sizeof to);

Here, to will be constructed from from without invoking a copy-constructor, so To obviously has to be trivially-copyable. On the other hand, as per definition, &from is taken as const void*, so none of its constructors would have to be called anyways. Therefore, as far as I can see, From does not really need to be trivially-copyable. And indeed, std::memcpy does not require anything to be trivially-copyable, but std::bit_cast does.

Why does the standard then impose this additional requirement and can it be safely avoided?

Note: this thread discusses why using std::memcpy on non-trivially-copyable types is undefined behavior, but only for the cases where To and From are the same type, and thus, requiring To to be trivially-copyable requires From to be trivially-copyable as well.

Why does C++ std::bit_cast require both To and From to be trivially-copyable? For example:

From from{};
To to;
static_assert(sizeof to == sizeof from);
std::memcpy(&to, &from, sizeof to);

Here, to will be constructed from from without invoking a copy-constructor, so To obviously has to be trivially-copyable. On the other hand, as per definition, &from is taken as const void*, so none of its constructors would have to be called anyways. Therefore, as far as I can see, From does not really need to be trivially-copyable. And indeed, std::memcpy does not require anything to be trivially-copyable, but std::bit_cast does.

Why does the standard then impose this additional requirement and can it be safely avoided?

Note: this thread discusses why using std::memcpy on non-trivially-copyable types is undefined behavior, but only for the cases where To and From are the same type, and thus, requiring To to be trivially-copyable requires From to be trivially-copyable as well.

Share Improve this question edited 13 hours ago Toby Speight 30.9k49 gold badges74 silver badges113 bronze badges asked 15 hours ago lobelklobelk 4542 silver badges10 bronze badges 14
  • 1 Note that memcpy's page on cppreference (the one that you linked to) no longer indicates that being trivially copiable is required. The other question reports that, but it was written in 2015. On the other hand, en.cppreference/w/cpp/types/is_trivially_copyable does say that "Objects of trivially-copyable types that are not potentially-overlapping subobjects are the only C++ objects that may be safely copied with std::memcpy", so cppreference seems to be inconsistent here. – Fabio says Reinstate Monica Commented 15 hours ago
  • 1 The thread you linked is not limited to objects of the same type. The question only uses that for its swapping example, but the answers apply to all types. – molbdnilo Commented 13 hours ago
  • When you see "trivially copyable", think of that as meaning "suitable for a bitwise copy". – Eljay Commented 13 hours ago
  • 1 @Eljay Yes, that is what is basically meant by "trivially-copyable". The problem here is that From does not need to be suitable for a bitwise copy. It only needs to be suitable for a bitwise read. As of now, bitwise read is technically a UB, but that is considered a language defect to be corrected, per the paper P1839R7 – lobelk Commented 13 hours ago
  • 1 @ÖöTiib "because the standard says so" is not helpful -- they want to know the rationale for this in the standard. – Barmar Commented 2 hours ago
 |  Show 9 more comments

1 Answer 1

Reset to default 1

There isn’t any one strong reason, just several general concerns:

  1. symmetry (generally one would expect to be able to reverse such a function)
  2. virtual pointers (though, like other pointers, these could be disallowed only for constant evaluation)
  3. compatibility with std::memcpy (in the absence of any other official means of accessing and imbuing object representations)
发布评论

评论列表(0)

  1. 暂无评论