map::emplace() with a custom value type

后端 未结 2 1628
陌清茗
陌清茗 2021-02-12 11:39

I\'m having trouble using map::emplace(). Can anyone help me figure out the right syntax to use? I am effectively trying to do the same thing as in this example. He

相关标签:
2条回答
  • 2021-02-12 12:18

    GCC 4.7 does not have full support of emplace functions.

    You can see C++11 support in GCC 4.7.2 here.

    0 讨论(0)
  • 2021-02-12 12:27

    A container's emplace member constructs an element using the supplied arguments.

    The value_type of your map is std::pair<const int, Foo> and that type has no constructor taking the arguments { 5, 5, 'a', 'b' } i.e. this wouldn't work:

    std::pair<const int, Foo> value{ 5, 5, 'a', 'b' };
    map.emplace(value);
    

    You need to call emplace with arguments that match one of pair's constructors.

    With a conforming C++11 implementation you can use:

    mymap.emplace(std::piecewise_construct, std::make_tuple(5), std::make_tuple(5, 'a', 'b'));
    

    but GCC 4.7 doesn't support that syntax either (GCC 4.8 will when it's released.)

    0 讨论(0)
提交回复
热议问题