`std::unordered_map` emplace error without `std::piecewise_construct`

有些话、适合烂在心里 提交于 2019-12-11 09:54:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!