Question

I have a piece of code that works fine with MSVC but fails to compile with clang++

void MyCass::someMethod()
{
   std::wstring key(...);
   auto& refInstance = m_map.find(key); // error here
}

where m_map is defined as

std::map<const std::wstring, std::shared_ptr<IInterface>> m_map;

and clang complains

non-const lvalue reference cannot bind to incompatible temporary

I somewhat understand that a temporary is being created but not sure how to go fixing this. Any ideas?

Was it helpful?

Solution

rvalues cannot bind to non-const references. MSVC has an "extension that allows that. To be standards compliant, you need

const auto& refInstance = m_map.find(key);

But this returns an iterator. It is unusual to use references to iterators. Values are fine:

auto refInstance = m_map.find(key);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top