- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
写 const auto& [a, b] = f();
是否保证延长从 f()
返回的对象的生命周期,或者至少是对象 a
和 b
是绑定(bind)的吗?通读the proposal我没有在语言中看到任何明显的东西来确保它确实如此,除非它只是被其他东西所覆盖。但是,以下内容不会延长临时的生命周期,所以我看不出它会如何被覆盖:
const auto& a = std::get<0>(f());
在论文的顶部,它似乎暗示它已被覆盖
the cv-qualifiers and ref-qualifier of the decomposition declaration are applied to the reference introduced for the initializer, not for the individual member aliases
但在实际标准的建议措辞中,我看到的最接近的提及如下,虽然我不确定如何阅读它以获得我正在寻找的保证:
if e is an unparenthesized id-expression naming an lvalue or reference introduced from the identifier-list of a decomposition declaration, decltype(e) is the referenced type as given in the specification of the decomposition declaration
gcc 和 clang 似乎都根据 wandbox experiment 将返回的对象的生命周期延长到作用域结束。 .一个 uglier one为我自己的类型实现所有的花里胡哨似乎可以延长外部对象及其其他数据成员的生命周期。
尽管几乎可以肯定作者的意图,但我想确定该语言保证这是安全的。
最佳答案
是的。诀窍是要意识到,尽管外观如此,[
之前的结构化绑定(bind)声明部分并不适用于 identifier-list 中的名称。它们改为应用于声明隐式引入的变量。 [dcl.struct.bind]/1 :
First, a variable with a unique name
e
is introduced. If the assignment-expression in the initializer has array typeA
and no ref-qualifier is present,e
has typecv A
and each element is copy-initialized or direct-initialized from the corresponding element of the assignment-expression as specified by the form of the initializer. Otherwise,e
is defined as-if byattribute-specifier-seqopt decl-specifier-seq ref-qualifieropt
e
initializer ;where the declaration is never interpreted as a function declaration and the parts of the declaration other than the declarator-id are taken from the corresponding structured binding declaration.
然后将名称定义为 e
元素的别名或绑定(bind)到在 e
上调用 get
的结果的引用。
在您的示例中,就好像(假设 f
返回一个二元素 std::tuple
):
const auto& e = f(); // 1
using E = remove_reference_t<decltype((e))>;
std::tuple_element<0, E>::type& a = get<0>(e);
std::tuple_element<1, E>::type& b = get<1>(e);
(除了 decltype(a)
和 decltype(b)
得到特殊处理以隐藏它们的引用性。)
很明显,第 1 行确实延长了 f
的返回值的生命周期。
关于c++ - 结构化绑定(bind)中的 const 引用是否会延长分解对象的生命周期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41176231/
我有一个网站,用户可以在其中提交游戏报告。我目前结合使用 markdown 和 HTMLPurifier 来允许有限的标记。 我想添加内联照片支持,从用户图库中绘制图像。 我用一些 JS 构建了一个图
Name = (src.Client.Account.Rank > 65 ? src.Client.Account.Rank > 2 ? "$" : "@" : "") + src.Name, 这
在下面的例子中: http://coliru.stacked-crooked.com/a/7a1df22bb73f6030 struct D{ int i; auto test2
有没有办法延长 Firebase SMS 的过期时间?似乎只有一分钟左右有效,有时需要比这更长的时间才能收到短信。 更新:我正在通过 Web API 发送短信 最佳答案 将此视为目前的最新答案,今天已
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 9年前关闭。 Improve this que
我正在绘制相同的 密度图 使用 基础绘图系统和 ggplot2 . 中的密度图基础绘图系统有光滑的尾部: d = density(iris$Sepal.Length) plot(d) 中的密度图ggp
大家好,我认识的每个人都问这个问题很基础而且很愚蠢,但这个问题让我很烦。如果我有以下代码。 var timerVal = 900000 function myFunction() { setTim
当我在 Rust 0.12.0 中编译以下代码时,出现以下错误: error: borrowed value does not live long enough let _ = match re.ca
我正在尝试将一个系列解析为 token 树,但是当我尝试实现我的解析特征时,我收到了与引用生命周期相关的错误。我认为创建一个盒装版本可以解决任何与引用计数或生命周期有关的问题。代码如下。 impl P
当我在 Rust 0.12.0 中编译以下代码时,出现以下错误: error: borrowed value does not live long enough let _ = match re.ca
我在方法中有以下代码: int64_t delayInSeconds = 2.0f; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
当我按下主页按钮时,我的应用程序正确暂停,但只持续几分钟。如果我离开它一会儿然后返回它,应用程序会重新启动。 我没有在我的应用程序中运行任何后台任务,其他应用程序保持暂停状态的时间比我的要长得多。 这
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 9 年前。 Improve this qu
我需要服务停止足够长的时间。大约 5-10 分钟。现在在 Windows 10 上,我的服务系统在 60 秒后终止,而没有等待它正确完成。使用网络函数 RequestAdditionalTime 看到
如何延长windows app证书的有效期?我们正在加载应用程序/主要障碍是证书每年都会过期,通过 GP 更新证书很麻烦,因为我们的环境中已经存在问题。我们希望将其延长至少 5 年。 我设法找到了关于
最近我看到了这个: 在 2037 年之前,我的任何应用都不太可能存活(而且我存活了下来,所有依赖它的东西都存活了),我的应用是否会因为需要新证书而必须使用新的包名称重新发布? 没有办法延长证书吗? 最
有什么方法可以延长 keystore 或证书的时间有效性吗? 我已经创建了一个有效期为 10 年的 keystore 。但是,由于谷歌市场的限制,我需要 30 年的有效期。 最佳答案 证书有效性是证书
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关于您编写的代码问题的问题必须在问题本身中描述具体问题 — 并且包括有效代码 以重现它。参见 SSC
我 3 天前注册了 30 天的 Azure 试用版。我有 2 个虚拟机。今天,我的管理门户中弹出 2 条消息。 您的免费试用将在 25 天后到期。单击此处立即升级。 根据您的使用历史记录(21.52
我是一名优秀的程序员,十分优秀!