问题描述
我使用 -std=c++17
构建它并尝试了 g++
和 clang++
,例如 clang++ -std=c++17 <file>
。它显示了相同的结果。
unordered_map
将 unordered_map 传递给函数中的右值参数并将其分配给另一个引用。并且内存不允许带出函数。
#include <iostream>
#include <string>
#include <unordered_map>
#include <list>
using namespace std;
typedef unordered_map<string,string> kw_t;
struct Bar {
kw_t &foo;
string bar;
};
list<Bar> bars;
void test(kw_t &&foo) {
cout << &foo["a"] << endl;
bars.emplace_back(Bar { .foo = foo,.bar = "bar" });
cout << &bars.front().foo["a"] << endl;
}
int main()
{
test({{"a","b"}});
cout << &bars.front().foo["a"] << endl;
return 0;
}
它有输出:
0x1f3ded8
0x1f3ded8
[1] 9776 segmentation fault (core dumped) ./a.o
列表
#include <iostream>
#include <list>
#include <string>
using namespace std;
typedef list<string> args_t;
struct Bar {
args_t &foo;
string bar;
};
list<Bar> bars;
void test(args_t &&foo) {
cout << &foo.front() << endl;
bars.emplace_back(Bar { .foo = foo,.bar = "bar" });
cout << &bars.front().foo.front() << endl;
}
int main()
{
test({"a","b"});
cout << &bars.front().foo.front() << endl;
return 0;
}
打印出来了:
0x15a7ec0
0x15a7ec0
0x15a7ec0
为什么第二个可以,第一个不行?
编辑 1:
clang 7.1.0 版
g++ (GCC) 9.3.0
解决方法
为什么第二个可以,第一个不行?
在这两种情况下,程序的行为都是未定义的。因此,它“可以”、“可能”或“被允许”看起来起作用(对于您认为“工作”的任何事物)。或者不“工作”。或者有任何其他行为。
澄清一下,引用 bars.front().foo
绑定到的临时对象的生命周期已经结束,因此引用无效。通过无效引用调用成员函数会导致未定义的行为。
这是编译器的错误行为吗?
没有。编译器运行正常。但是,您的程序已损坏。