23

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. Here is my version:

#include <map>
using namespace std;

class Foo
{
  // private members

  public:
    Foo(int, char, char) /* :init(), members() */ {  }

    // no default ctor, copy ctor, move ctor, or assignment
    Foo() = delete;
    Foo(const Foo&) = delete;
    Foo(Foo &&) = delete;
    Foo & operator=(const Foo &) = delete;
    Foo & operator=(Foo &&) = delete;
};


int main()
{
  map<int, Foo> mymap;
  mymap.emplace(5, 5, 'a', 'b');

  return 0;
}

Under GCC 4.7 (with the -std=c++11 flag), I am erroring out on the emplace line. The example I linked above doesn't say anything about how to deal with custom types instead of primitives.

Nicu Stiurca
  • 8,747
  • 8
  • 40
  • 48
  • See also http://stackoverflow.com/questions/13689482/use-of-emplaceargs-in-associative-containers – Cubbi Dec 28 '12 at 22:57

2 Answers2

26

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.)

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • This seems to work fine, even on GCC 4.7. Thanks. FWIW, I used `std::forward_as_tuple()` instead of `std::make_tuple()`, because I think it should be slightly better optimized, but I'm not really sure what the difference is. – Nicu Stiurca Dec 28 '12 at 22:30
  • 1
    `forward_as_tuple(5, 'a', 'b')` creates `tuple` and `make_tuple(5, 'a', 'b')` creates `tuple`. In general `forward_as_tuple` avoids making copies of its arguments and forwards references, but in this case (with `int` and `char` arguments) the copies created by `make_tuple` are probably cheaper than creating references. It's also less typing :) – Jonathan Wakely Dec 29 '12 at 00:25
  • 3
    As for C++17, `map.try_emplace(5, 5,'a','b') `; can be used, too. – Ghoster Mar 19 '20 at 00:51
3

GCC 4.7 does not have full support of emplace functions.

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

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
Rapptz
  • 20,807
  • 5
  • 72
  • 86
  • I was afraid of that. Is what I tried the correct syntax though? Ie, would it work on some other compiler? – Nicu Stiurca Dec 28 '12 at 20:15
  • Also, `emplace()` works fine with `map` like in the cplusplus.com example. – Nicu Stiurca Dec 28 '12 at 20:20
  • @SchighSchagh You might need to `forward_as_tuple`, I'm not sure as I have never used `emplace`. – Rapptz Dec 28 '12 at 20:24
  • It is a valid reason that the call would not work. The fact that you have another is not justification for downvote. – Puppy Dec 28 '12 at 20:53
  • @SchighSchagh: the cplusplus example doesn't work on `g++-4.7.2` also, I just tested it. – KillianDS Dec 28 '12 at 20:54
  • 2
    The linked document is out of date, GCC trunk (which that page refers to) _does_ support `map::emplace`, the correct link for GCC 4.7 is http://gcc.gnu.org/onlinedocs/gcc-4.7.2/libstdc++/manual/manual/status.html#status.iso.2011 – Jonathan Wakely Dec 28 '12 at 21:02
  • @JonathanWakely good to know. The link I gave was a bookmark I had. – Rapptz Dec 28 '12 at 21:05