Why my const ref becomes invalid in this code and how to avoid this? I can't copy, this is the bottleneck in my application.
class Foo {
public:
const std::string& string() const {
return string;
}
private:
std::string string = "asdf";
};
Foo foo;
std::vector<std::pair<const std::string&, int>> invalid;
for (int i = 0; i < 5; i++) {
invalid.emplace_back(std::make_pair(foo.string(), i);
// after this line invalid[i].first is invalid
}