Clang error: non-const lvalue link cannot communicate with incompatible temporary

I have a code snippet that works fine with MSVC but doesn't 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 understand somewhat that a temporary one is being created, but I'm not sure how to fix it. Any ideas?

+4
source share
1 answer

rvalues ​​cannot communicate with non-constant links. MSVC has an extension that allows this. To meet the standards you need

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

But this returns an iterator. It is unusual to use references to iterators. Values ​​in order:

auto refInstance = m_map.find(key);
+6
source

Source: https://habr.com/ru/post/1535990/


All Articles