参数是否破坏发生之前返回? [英] Does Destruction of Arguments Occur Prior to Return?
问题描述
OK我有这一系列事件:
OK I have this sequence of events:
- 我构建一个r值对象
- 我传递一个迭代器的r值对象到一个函数作为参数
- 的功能在这个迭代器 运行
- 该函数返回该迭代通过值
- 取消引用我的迭代器
我不知道是什么原因导致的r值对象的清理,这是该行的终止?
I don't know what causes cleanup of the r-value object, is it the termination of that line?
好了,现在的具体情况,我想拿出这个问题的一个更好的答案:字符串乘法C ++ 一>和我有code:
OK, now for specifics, I'm trying to come up with a better answer for this question: string Multiplication in C++ And I have the code:
const auto bar = 13U;
const char multiplicand[] = "0, ";
const auto length = strlen(multiplicand);
const string foo(&*generate_n(string(bar * length, '\0').begin(), bar * length, [&]() {
static auto i = 0U;
return multiplicand[i++ % length];
}) - bar * length);
所以我想知道什么时候字符串
这里面 generate_n
构造应该被销毁。顺便说一句,这似乎对GCC 5.1做工精细: http://ideone.com/Y8rDs5 但我可以只是越来越未定义的行为。这是由以下事实暗示上Visual Studio中code段错误2015年
So I want to know when the string
that's constructed inside generate_n
should be destroyed. Incidentally this seems to work fine on gcc 5.1: http://ideone.com/Y8rDs5 But I could just be getting undefined behavior. This is implied by the fact that the code segfaults on Visual Studio 2015.
推荐答案
临时变量,如字符串(巴*长度,'\\ 0')
在结束时被销毁在完全的前pression。完整的前pression是常量字符串富
的初始化。因此,临时字符串不会被富
收益的构造函数之前销毁。
Temporaries such as string(bar * length, '\0')
are destroyed at the end of the full expression. The full expression is the initializer of const string foo
. Hence, the temporary string will not be destroyed before the ctor of foo
returns.
这篇关于参数是否破坏发生之前返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!