I'm confused about some code involving references and std::reference_wrapper and I'm not clear at all whether this is my fault since I'm misunderstanding how reference wrapper works or if I hit a compiler error.
I have a simple map, pairing a reference to an object from a complex class to a value:
std::unordered_map<Object const &obj, int32_t value> myMap;
(for simplicity, I explicitly left out the hash and equal functors required by the map to compile)
Since I can't directly use the reference in the map, I use a reference wrapper to it:
std::unordered_map<std::reference_wrapper<const Object> obj, int32_t value> myMap;
Now, my map is populated in a function such as:
void myFunction(Object const &obj, int32_t value)
{
...
myMap.emplace(std::make_pair(obj, value));
}
but that code, although compiles, doesn't work. However, this one works as expected:
void myFunction(Object const &obj, int32_t value)
{
...
myMap.emplace(std::pair<std::reference_wrapper<const Object>, int32_t>(obj, value));
}
(notice in this second version I'm still not explicitly building a reference wrapper to obj)
So, my doubt is:
Am I misunderstanding something about the usage of reference wrapper? There's no implicit conversion from reference to reference_wrapper? If not, why does the code compile in the first place?
Or is this a known issue/flaw in std::make_pair not being capable to correctly deduct the types passed to it?