Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang error: non-const lvalue reference cannot bind to incompatible temporary

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?

like image 880
randomThought Avatar asked Apr 10 '14 19:04

randomThought


1 Answers

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);
like image 77
juanchopanza Avatar answered Nov 10 '22 00:11

juanchopanza