问题
I have the following code:
#include <complex>
#include <tuple>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<string, complex<double>> um;
um.emplace(piecewise_construct, make_tuple("test1"), make_tuple(1,1));// works
// um.emplace("test2", {1,1}); // error here
complex<double> z{1,1};
um.emplace("test3", z); // this works
}
I do not completely understand why am I getting an error on the commented line. The unordered_map is emplacing a pair<string, complex<double>>, and complex<double> has a initialization list type constructor, why cannot then the pair can be constructed in place? If I replace the init-list {1,1} with a previously constructed complex<double>, then it works.
来源:https://stackoverflow.com/questions/24743599/stdunordered-map-emplace-error-without-stdpiecewise-construct