I'd think that the VS2019 suggestion would create a dangling reference situation, but I tested it out, and it seems to work. What is happening here?
template<typename MessageType>
class Queue {
inline static std::vector<MessageType> messages;
public:
static bool isEmpty() {
return messages.size() == 0;
}
template <typename... Args>
static void emplace(Args&&... args) {
messages.emplace_back(std::forward<Args>(args)...);
}
static MessageType pop() {
auto const& val = messages.back();
messages.pop_back();
return val;
}
};
It looks like the last message stays alive long enough to be copied into the return value. Is this good practice?