0

Suppose we have a map

using namespace std;
unordered_map<tuple<string,string>,string> map;

If I do

string a("a"), b("b");
map.emplace( std::tie(a,b), "c" );

are a and b actually copied, or does the map only keep their references as did the tuple produced with tie?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
SU3
  • 5,064
  • 3
  • 35
  • 66
  • 1
    Actually copied. The key in the map is `tuple`, constructed from `tuple` you passed in. – Igor Tandetnik May 29 '15 at 01:25
  • Is there a way to prevent copying with using references instead of pointers? – SU3 May 29 '15 at 01:28
  • I suppose you could try `unordered_map,string>`. No idea if that would work. Strikes me as a rather strange thing to want to do. For one thing, you'd have to somehow keep a bunch of `string` variables alive for as long as the map refers to them. Sounds like an XY problem - what problem are you really trying to solve? – Igor Tandetnik May 29 '15 at 02:00
  • It relates to a [question](http://stackoverflow.com/questions/28866571) I asked some time ago. Whatever I tried, the [simplest implementation](https://github.com/ivankp/propmap_cpp11/blob/c0125f043991c29fccaf2779928ad4529eea4e5a/propmap.hh) is the fastest on benchmark, so I thought that maybe the map doesn't actually copy the tuple elements. – SU3 May 29 '15 at 04:24
  • @Igor: Please write answers in the _answer_ section.... – Lightness Races in Orbit Nov 12 '15 at 00:00

0 Answers0