【发布时间】:2021-11-23 15:33:37
【问题描述】:
我正在阅读type aliasing rules,但无法确定此代码中是否包含 UB:
std::vector<std::byte> vec = {std::byte{'a'}, std::byte{'b'}};
auto sv = std::string_view(reinterpret_cast<char*>(vec.data()), vec.size());
std::cout << sv << '\n';
我很确定它不会,但我经常对 C++ 感到惊讶。
reinterpret_cast 是否始终位于 char*、unsigned char* 和 std::byte* 之间?
此外,const 的 addition 在这种演员阵容中是否合法,例如:
std::array<char, 2> arr = {'a', 'b'};
auto* p = reinterpret_cast<const std::byte*>(arr.data());
再次,我怀疑它是合法的,因为它说
AliasedType 是 DynamicType 的(可能是 cv 限定的)有符号或无符号变体
但我想一劳永逸地确定reinterpret_casting。
【问题讨论】:
-
相关问题:What is the strict aliasing rule? 但是,这个问题不是很具体,因为它不区分 C 和 C++,尽管两种语言的规则不同。
-
如果我们假设
vec.data()给出了一个指向数组元素的指针,然后在sv中对该指针的reinterpret_cast<char*>-ed值进行指针运算,那么它就违反了timsong-cpp.github.io/cppwp/n4868/expr.add#6.sentence-1 . -
@LanguageLawyer 你能扩展一下吗?
-
扩展什么?指向
std::byte类型对象的char*类型指针不能用于指针算术。如果您不将所有内容隐藏在库函数调用后面并明确公开它们所做的基本事情,那么推理您的代码会容易得多。 -
我看不出图书馆在哪里可以做这样的算术。
std::byte b[2]; reinterpret_cast<char*>(&b[1]) - reinterpret_cast<char*>(&b[1]);我不认为这是 UB。vec.data()提供指向数组元素的指针,但它是否被重新解释为char*。例如,这个char*可以从另一个char*中减去,不是吗?
标签: c++ language-lawyer reinterpret-cast