- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
让我们编译下面的顶级声明
const int& ri = 5;
与 clang++
.与 -std=c++14
在它下面将临时对象(和代表引用的指针)放入 .rodata
中部分:
.type _ZGR2ri_,@object # @_ZGR2ri_
.section .rodata,"a",@progbits
.p2align 2
_ZGR2ri_:
.long 5 # 0x5
.size _ZGR2ri_, 4
.type ri,@object # @ri
.globl ri
.p2align 3
ri:
.quad _ZGR2ri_
.size ri, 8
但是如果我们把标准版改成-std=c++17
(或以上),对象将被放入 .data
部分(尽管指针仍在 .rodata
中):
.type _ZGR2ri_,@object # @_ZGR2ri_
.data
.p2align 2
_ZGR2ri_:
.long 5 # 0x5
.size _ZGR2ri_, 4
.type ri,@object # @ri
.section .rodata,"a",@progbits
.globl ri
.p2align 3
ri:
.quad _ZGR2ri_
.size ri, 8
这种行为的原因是什么?这是一个错误吗?事实上,它仍然取代了 ri
的所有用途在同一 TU 中按其初始值 5
表明这是一个错误。
我的假设是 [dcl.init.ref]/5.2
If the converted initializer is a prvalue, its type T4 is adjusted to type “cv1 T4” ([conv.qual]) and the temporary materialization conversion is applied.
它天真地丢弃(或者更确切地说不添加)cv1
- 来自(到)纯右值类型的限定符。
有趣的是,如果用非引用相关但可转换类型的纯右值替换初始化表达式
const int& ri = 5.0;
它开始放置值为5
的对象进入.rodata
再次节。
现在标准中是否有任何内容需要这种可变性?换句话说:
ri
指定的对象可以通过符合代码修改吗? (显然,涉及 UB 的代码可能会尝试更改它,并且编译器不需要做出努力来允许这样做)ri
临时“别名”(“引用是别名”)大小的对象那是 sizeof (int)
?最佳答案
分析一下
const int& ri = 5;
来自 C++ 草案:引用的初始化 [dcl.init.ref]/5
A reference to type “cv1 T1” is initialized by an expression of type “cv2 T2” as follows:
这里cv1 = const
, T1 = int
, cv2 = "", T2 = int
跳过不适用的条款,我们来到这里[dcl.init.ref]/5.3 :
Otherwise, if the initializer expression (5.3.1) is an rvalue (but not a bit-field) (...) and “cv1 T1” is reference-compatible with “cv2 T2”, or (...) then the value of the initializer expression (...) is called the converted initializer.
转换后的初始值是 5 个纯右值。
If the converted initializer is a prvalue, its type T4 is adjusted to type “cv1 T4” ([conv.qual]) and the temporary materialization conversion ([conv.rval]) is applied. In any case, the reference is bound to the resulting glvalue (...)
cv1 T4 = const int
因此创建了一个 const int
类型的对象并将引用绑定(bind)到它。
“临时物化转换”是此处解释的新概念 [conv.rval] :
A prvalue of type T can be converted to an xvalue of type T. This conversion initializes a temporary object ([class.temporary]) of type T from the prvalue by evaluating the prvalue with the temporary object as its result object, and produces an xvalue denoting the temporary object. T shall be a complete type.
所以我们有一个转换 prvalue -> xvalue -> lvalue。
临时的生命周期在[class.temporary]/6中描述。 :
The temporary object to which the reference is bound or (...) persists for the lifetime of the reference if the glvalue to which the reference is bound was obtained through one of the following:
(6.1) a temporary materialization conversion ([conv.rval]), (...)
情况就是这样,临时对象的生命周期“在引用的生命周期内持续存在”。
A program may end the lifetime of any object by reusing the storage which the object occupies
但不是每个对象存储都可以这样使用:[basic.memobj]/10
Creating a new object within the storage that a const complete object with static, thread, or automatic storage duration occupies, or within the storage that such a const object used to occupy before its lifetime ended, results in undefined behavior.
存储期限在这里定义[basic.stc]
The storage duration is the property of an object that defines the minimum potential lifetime of the storage containing the object. The storage duration is determined by the construct used to create the object and is one of the following: (1.1) static storage duration (1.2) thread storage duration (1.3) automatic storage duration (1.4) dynamic storage duration 2 Static, thread, and automatic storage durations are associated with objects introduced by declarations ([basic.def]) and implicitly created by the implementation.
但是文本只提到了变量,没有提到对象。我没有看到临时文件的存储期限在哪里定义!
编辑: @LanguageLawyer 指出了这个核心缺陷:
1634. Temporary storage duration
The apparent intent of the reference to 15.2 [class.temporary] is that a temporary whose lifetime is extended to be that of a reference with one of those storage durations is considered also to have that storage duration.
(...) the specification of lifetime extension of temporaries (also in 15.2 [class.temporary] paragraph 5) does not say anything about storage duration. Also, nothing is said in either of these locations about the storage duration of a temporary whose lifetime is not extended.
所以规范中确实有遗漏的部分;实现创建的这些对象的生命周期没有明确规定。 C++ 中的生命周期规范很困难,正如您从更新的标准中对生命周期、 union 、子对象和“嵌套”的规范中添加的许多内容所看到的那样;其中一些新条款甚至适用于不使用新 C++ 功能的代码,这些代码旨在在 ARM 的准标准时代得到支持(但没有很好地描述),例如代码只做改变“union 的活跃成员”。
如果按照 DR 声明的方式解释规范,则 const int
临时值 5 的生命周期将具有静态存储持续时间; 其内存不可合法修改,可以放在只读部分。
(另一种解决方案:委员会还可以为临时对象创建一个特定的存储类。)
关于C++17:编译器为(静态存储持续时间)const 引用绑定(bind)创建的临时对象(和存储)是否可修改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54413490/
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: template pass by value or const reference or…? 以下对于将函数
我用相同的参数列表重载了一个运算符两次。但返回类型不同: T& operator()(par_list){blablabla} const T& operator()(par_list){bla
假设我有实现接口(interface) I 的 Activity A。我的 ViewModel 类 (VM) 持有对实现接口(interface) I 的对象的引用: class A extends
PHP 如何解释 &$this ?为什么允许? 我遇到了以下问题,这看起来像是 PHP 7.1 和 7.2 中的错误。它与 &$this 引用和跨命名空间调用以及 call_user_func_arr
谁能解释一下下面“&”的作用: class TEST { } $abc =& new TEST(); 我知道这是引用。但是有人可以说明我为什么以及什么时候需要这样的东西吗?或者给我指向一个对此有很好解
引用变量是一个别名,也就是说,它是某个已存在变量的另一个名字。一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量。 C++ 引用 vs 指针 引用很容易与指针混淆,它们之间有三
目录 引言 背景 结论 引言 我选择写C++中的引用是因为我感觉大多数人误解了引用。而我之所以有这个感受是因为我主持过很多C++的面试,并且我很少
Perl 中的引用是指一个标量类型可以指向变量、数组、哈希表(也叫关联数组)甚至函数,可以应用在程序的任何地方 创建引用 定义变量的时候,在变量名前面加个 \,就得到了这个变量的一个引用 $sc
我编写了一个将从主脚本加载的 Perl 模块。该模块使用在主脚本中定义的子程序(我不是维护者)。 对于主脚本中的一个子例程,需要扩展,但我不想修补主脚本。相反,我想覆盖我的模块中的函数并保存对原始子例
我花了几个小时试图掌握 F# Quotations,但我遇到了一些障碍。我的要求是从可区分的联合类型中取出简单的函数(只是整数、+、-、/、*)并生成一个表达式树,最终将用于生成 C 代码。我知道使用
很多时候,问题(尤其是那些标记为 regex 的问题)询问验证密码的方法。似乎用户通常会寻求密码验证方法,包括确保密码包含特定字符、匹配特定模式和/或遵守最少字符数。这篇文章旨在帮助用户找到合适的密码
我想通过 MIN 函数内的地址(例如,C800)引用包含文本的最后一个单元格。你能帮忙吗? Sub Set_Formula() ' ----------------------------- Dim
使用常规的 for 循环,我可以做类似的事情: for (let i = 0; i < objects.length; i++) { delete objects[i]; } 常规的 for-
在 Cucumber 中,您定义了定义 BDD 语法的步骤;例如,您的测试可能有: When I navigate to step 3 然后你可以定义一个步骤: When /^I navigate t
这是什么UnaryExpression的目的,以及应该怎样使用? 最佳答案 它需要一个 Expression对象并用另一个 Expression 包裹它.例如,如果您有一个用于 lambda 的表达式
给出以下内容 $("#identifier div:first, #idetifier2").fadeOut(300,function() { // I need to reference jus
我不知道我要找的东西的正确术语,但我要找的是一个完整的引用,可以放在双引号之间的语句,比如 *, node()、@* 以及所有列出的 here加上任何其他存在的。 我链接到的答案提供了一些细节,但还
This question's answers are a community effort。编辑现有答案以改善此职位。它当前不接受新的答案或互动。 这是什么? 这是常见问答的集合。这也是一个社区Wi
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
考虑下一个代码: fn get_ref(slice: &'a Vec, f: fn(&'a Vec) -> R) -> R where R: 'a, { f(slice) } fn m
我是一名优秀的程序员,十分优秀!