C++11 隐式转换共享\u ptr到bool
我想知道为什么共享_ptr的bool转换运算符在C++11 隐式转换共享\u ptr到bool,c++11,shared-ptr,implicit-conversion,C++11,Shared Ptr,Implicit Conversion,我想知道为什么共享_ptr的bool转换运算符在if语句中工作得很好,但在对(bool)返回值的隐式转换中却不行 std::shared_ptr<const OfflineContainer> m_pContainer; bool MyClass::IsInitialized() const { if (m_pContainer) // compi;es return true; return m_pContainer; // won't c
if语句
中工作得很好,但在对(bool)返回值的隐式转换中却不行
std::shared_ptr<const OfflineContainer> m_pContainer;
bool MyClass::IsInitialized() const
{
if (m_pContainer) // compi;es
return true;
return m_pContainer; // won't compile
}
std::shared_ptr m_pContainer;
bool MyClass::IsInitialized()常量
{
if(m_pContainer)//compi;es
返回true;
返回m_pContainer;//不会编译
}
编译器消息:
错误C2440“return”:无法从“const std::shared_ptr”转换为“bool”
这是因为被标记为explicit
。这意味着您不能进行隐式转换(如return
语句中所做的)