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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With