Map :: emplace () with a custom value type

I'm having problems using map::emplace() . Can someone help me figure out the correct syntax? I am effectively trying to do the same 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; } 

In GCC 4.7 (with the flag -std=c++11 ) I am mistaken in the emplace line. The example I linked above does not say anything about how to handle custom types instead of primitives.

+18
c ++ gcc c ++ 11
Dec 28
source share
2 answers

The emplace container emplace creates the element using the provided arguments.

value_type your map is std::pair<const int, Foo> and this type does not have a constructor that takes arguments { 5, 5, 'a', 'b' } ie, this will not 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 the pair constructors.

With the appropriate 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 also does not support this syntax (GCC 4.8 will be released.)

+19
Dec 28 '12 at 20:48
source share

GCC 4.7 does not have full support for emplace features.

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

+2
Dec 28
source share



All Articles