- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
注:这个问题已被重命名和减少,以使其更具针对性和可读性。大多数评论都引用了旧文本。
根据标准,不同类型的对象不能共享相同的内存位置。所以这不合法:
std::array<short, 4> shorts;
int* i = reinterpret_cast<int*>(shorts.data()); // Not OK
char
的指针访问任何对象。或
unsigned char
:
int i = 0;
char * c = reinterpret_cast<char*>(&i); // OK
char * c = read_socket(...);
unsigned * u = reinterpret_cast<unsigned*>(c); // huh?
最佳答案
由于涉及指针转换,您的某些代码存在问题。请记住,在这些情况下 reinterpret_cast<T*>(e)
具有 static_cast<T*>(static_cast<void*>(e))
的语义因为所涉及的类型是标准布局。 (实际上,我建议您在处理存储时始终通过 static_cast
使用 cv void*
。)
仔细阅读标准表明,在指针转换为或来自 T*
期间假设确实存在一个实际对象 T*
涉及——这在你的一些片段中很难实现,即使在“作弊”时也是如此,这要归功于所涉及的类型的琐碎(稍后会详细介绍)。然而,这不是重点,因为......
别名与指针转换无关。 这是 C++11 文本,概述了通常称为“严格别名”规则的规则,来自 3.10 Lvalues and rvalues [basic.lval]:
10 If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:
- the dynamic type of the object,
- a cv-qualified version of the dynamic type of the object,
- a type similar (as defined in 4.4) to the dynamic type of the object,
- a type that is the signed or unsigned type corresponding to the dynamic type of the object,
- a type that is the signed or unsigned type corresponding to a cv-qualified version of the dynamic type of the object,
- an aggregate or union type that includes one of the aforementioned types among its elements or non-static data members (including, recursively, an element or non-static data member of a subaggregate or contained union),
- a type that is a (possibly cv-qualified) base class type of the dynamic type of the object,
- a char or unsigned char type.
magic_cast<T*>(p)
其中“以某种方式”将指针转换为另一种指针类型。通常这将是
reinterpret_cast
,在某些情况下会产生未指定的结果,但正如我之前解释的那样,对于指向标准布局类型的指针,情况并非如此。那么很明显,您的所有片段都是正确的(用
reinterpret_cast
替换
magic_cast
),因为
magic_cast
的结果不涉及任何左值。 .
magic_cast
的片段,但我认为这是正确的:
// assume constexpr max
constexpr auto alignment = max(alignof(int), alignof(short));
alignas(alignment) char c[sizeof(int)];
// I'm assuming here that the OP really meant to use &c and not c
// this is, however, inconsequential
auto p = magic_cast<int*>(&c);
*p = 42;
*magic_cast<short*>(p) = 42;
为了证明我的推理,假设这个表面上不同的片段:
// alignment same as before
alignas(alignment) char c[sizeof(int)];
auto p = magic_cast<int*>(&c);
// end lifetime of c
c.~decltype(c)();
// reuse storage to construct new int object
new (&c) int;
*p = 42;
auto q = magic_cast<short*>(p);
// end lifetime of int object
p->~decltype(0)();
// reuse storage again
new (p) short;
*q = 42;
这个片段是精心构建的。特别是在
new (&c) int;
我可以使用
&c
即使
c
由于 3.8 对象生命周期 [basic.life] 的第 5 段中规定的规则而被销毁。第 6 段对存储引用给出了非常相似的规则,第 7 段解释了变量、指针和引用在重用存储后用于引用对象的情况——我将它们统称为 3.8/5- 7.
&c
被(隐式)转换为
void*
,这是对尚未重用的存储指针的正确使用之一。同样
p
来自
&c
新前
int
被构造。它的定义或许可以在
c
销毁后移到。 ,取决于实现魔法的深度,但肯定不是在
int
之后构造:第 7 段将适用,这不是允许的情况之一。
short
的 build 对象也依赖于
p
成为指向存储的指针。
int
和
short
是微不足道的类型,我不必使用对析构函数的显式调用。我也不需要对构造函数的显式调用(也就是说,调用在
<new>
中声明的通常的标准位置 new )。从 3.8 对象生命周期 [basic.life]:
1 [...] The lifetime of an object of type T begins when:
- storage with the proper alignment and size for type T is obtained, and
- if the object has non-trivial initialization, its initialization is complete.
The lifetime of an object of type T ends when:
- if T is a class type with a non-trivial destructor (12.4), the destructor call starts, or
- the storage which the object occupies is reused or released.
q
之后,我最终得到了原始片段。
p
不能折叠。也就是说,以下内容绝对是错误的:
alignas(alignment) char c[sizeof(int)];
*magic_cast<int*>(&c) = 42;
*magic_cast<short*>(&c) = 42;
如果我们假设
int
对象是(简单地)用第二行构造的,那么这一定意味着
&c
成为指向已重用存储的指针。因此第三行是不正确的——尽管由于 3.8/5-7 而不是由于严格意义上的别名规则。
char c[sizeof(int)]
的内容。对象通过类型为
int
的泛左值,这不是允许的异常(exception)之一。相比之下,
*magic_cast<unsigned char>(&c) = 42;
会很好(我们假设一个
short
对象在第三行被简单地构造)。
*some_magic_pointer = foo;
时您很可能面临违反 3.8/5-7(无论该指针获得多么神奇)或别名规则的违反。这意味着也要存储 new 表达式的结果,因为一旦构造了对象,您很可能无法重用魔术指针——再次由于 3.8/5-7。
char
或
unsigned char
)很好,但是您甚至不使用
reinterpret_cast
或任何魔法。
static_cast
通过
cv void*
可以说很适合这项工作(尽管我确实觉得标准可以在那里使用一些更好的措辞)。
关于c++ - 允许使用 char* 别名 T*。反过来也允许吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12612488/
上下文:我已经在域根上设置了一个用于 GIS 测试和 dokuwiki 的虚拟机服务器。我想在子域上提供 gis Web 应用程序,这样 dokuwiki url 重命名就不会发生冲突(而且感觉更干净
这个问题在这里已经有了答案: If two variables point to the same object, why doesn't reassigning one variable affe
代码如下: Select Branch.BranchName, Sum(Inventory.OnHand) From Inventory, Branch Inventory.BranchNum = B
我的目标是创建一种“Javascript 库”,如果您可以这样调用它的话。我打算在浏览网页时使用它来操作页面,将其作为 greasemonkey 脚本动态加载。这个想法是将“win”映射到window
是否可以在 javascript 中以某种方式为本地变量分配别名/引用? 我的意思是类似 C 的东西: function foo() { var x = 1; var y = &x; y+
SQLite 别名 您可以暂时把表或列重命名为另一个名字,这被称为别名。使用表别名是指在一个特定的 SQLite 语句中重命名表。重命名是临时的改变,在数据库中实际的表的名称不会改变。 列别名用来
我有以下导入(在同一个 TS 源文件中): import {Vector as sourceVector} from "ol/source"; import {Vector} from "ol/lay
我想弄清楚 this 到底是什么行是在这个 github json 项目的 cmake 文件中, add_library(${NLOHMANN_JSON_TARGET_NAME} INTERFACE)
使用集合时如何创建别名 (AS)。 $collection->addAttributeToSelect('total_qty') 可以这样做: $collection->getSelect()->co
这个问题在这里已经有了答案: Pass an argument to a Git alias command (4 个回答) 去年关闭。 我的团队在不同的分支上工作,通常,我需要快速从分支切换来帮助他
我正在寻找一种向我的 vimrc 添加自定义命令或别名的方法,以便快速切换到 Vim 中的不同目录。我曾经设置过一些东西,但丢失了那个配置。这是用于 Linux 设置。 基本上,我想设置一个别名(如果
我使用 Delphi 和 Firebird 1.5 开发了一个应用程序,其中服务器与应用程序位于同一台计算机上。我现在正在将应用程序部署到另一个站点,其中 Firebird 服务器( super 服务
我创建了一个全新的 React 应用 create-react-app demo 我需要为某些目录/组件创建别名,例如: import { Header } from '@uicomponents'
我正在尝试打印有向图,并且不断更改各种节点属性,例如颜色和形状。有什么方法可以使用一次定义并用于多个节点的变量?理想情况下,我想要这样的东西: digraph g { building_color =
server { listen 80; server_name pwta; root html; location /test/{ alia
我想匹配多个数组,并在匹配时构建另一个数组。该键可以在任意数量的数组中匹配,也可以根本不匹配。 [ [{ 'a': 13 }, { 'b': 62 }, { 'c': 93 }, { 'd': 52
经过审查后,我需要更改一些代码并将所有更改代码重新推送到远程分支上。 所以我想使用别名(例如:git repushall)在 git 上自动化这些不同的命令: git add . git commit
我有一个这样的查询: select a1.name, b1.info from (select name, id, status from table1 a) as a1 righ
我想给一些 racket 2htdp 函数/宏起别名,这样我就可以为我的 child 将它们翻译成另一种语言。 我可以简单地用define给函数起别名。我在使用 big-bang 结构时遇到了麻烦;例
经过审查后,我需要更改一些代码并将所有更改代码重新推送到远程分支上。 所以我想使用别名(例如:git repushall)在 git 上自动化这些不同的命令: git add . git commit
我是一名优秀的程序员,十分优秀!