好的我有这一系列事件:
我不知道导致r值对象清理的原因是什么,是该行的终止?
好的,现在针对具体细节,我试图为这个问题找到一个更好的答案:string Multiplication in C++我有代码:
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);
所以我想知道在string
内构建的generate_n
何时应该被销毁。顺便说一下,这似乎在gcc 5.1上运行良好:{{ 3}}但我可能会得到未定义的行为。这是因为Visual Studio 2015上的代码段错误。
答案 0 :(得分:1)
完整表达式末尾会销毁string(bar * length, '\0')
等临时对象。完整表达式是const string foo
的初始化程序。因此,在foo
的ctor返回之前,临时字符串不会被销毁。