Suppose I have this code:
#include <string>
#include <unordered_set>
struct command_node {
std::string key, value;
};
bool operator==(const command_node &x, const command_node &y) {
return x.key == y.key;
}
namespace std {
template<>
struct hash<command_node> {
typedef command_node argument_type;
typedef size_t result_type;
size_t operator()(const command_node &x) const {
return std::hash<std::string>()(x.key);
}
};
}
using command_set = std::unordered_set<command_node>;
int main(int argc, char **argv)
{
command_set commands;
commands.emplace(
command_node{"system", "running"}
);
return EXIT_SUCCESS;
}
which just creates an unordered_list of a command_node
structs. The code is just for illustration.
Question: In main, this works (as shown above):
commands.emplace(
command_node{"system", "running"}
);
but this does not:
commands.emplace(
{"system", "running"}
);
yet if I replace emplace
with insert
, it works either way. In other words, this works:
commands.insert(
{"system", "running"}
);
Why does emplace not infer command_node
?