clang错误:非常量左值引用无法绑定到不兼容的临时
clang error: non-const lvalue reference cannot bind to incompatible temporary
我有一段代码可以很好地使用MSVC,但无法使用clang++进行编译
void MyCass::someMethod()
{
std::wstring key(...);
auto& refInstance = m_map.find(key); // error here
}
其中m_map被定义为
std::map<const std::wstring, std::shared_ptr<IInterface>> m_map;
叮当声抱怨
non-const lvalue reference cannot bind to incompatible temporary
我有点理解正在创建一个临时的,但不知道如何解决这个问题。有什么想法吗?
右值不能绑定到非常量引用。MSVC有一个"允许这样做的扩展。要符合标准,你需要
const auto& refInstance = m_map.find(key);
但这会返回一个迭代器。使用对迭代器的引用是不常见的。数值很好:
auto refInstance = m_map.find(key);
相关文章:
- 为什么结构化绑定不使用"auto&"返回对结构成员的引用,而是返回成员本身
- Boost 绑定和"result_type":不是成员,对 c++03 友好
- C++ - 如何在不静态的情况下将回调绑定到类方法?
- 为什么结构化绑定不支持可变数组?
- 没有类型结构绑定不起作用?
- 为什么右值不能绑定到非常量左值引用,除了写入临时无效的事实?
- 为什么基于范围的 for 循环中的结构化绑定只是一个副本而不是引用?
- 您自己的类型的结构化绑定,不是结构或元组(通过公共成员函数)
- 为什么我不能将常量左值引用绑定到返回 T&&&的函数?
- 为什么我不能绑定?
- QtSql:绑定不会改变SQLite的查询
- 为什么我不能在绑定中使用mem_fn函子?
- | 9 |错误:非静态数据成员'matrix :: row'| 9 |错误:数组绑定不是在']']'令牌|之前的整数常数
- 纹理绑定不起作用 / C++ / OpenGL
- 如果结构化绑定不能是 constexpr,为什么它们可以在 constexpr 函数中使用?
- boost::与boost:asio绑定在一起.boost::绑定不起作用,从示例中复制
- 绑定不是一个值,而是一个函数(获取该函数的值)
- 为什么c++ 17的结构化绑定不使用{}?
- 套接字绑定不会返回 int
- clang错误:非常量左值引用无法绑定到不兼容的临时