首页 > 解决方案 > 将 shared_ptr 隐式转换为 bool

问题描述

我想知道为什么 shared_ptr 的 bool 转换运算符在 an 中可以正常工作if-statement,但在到 (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
}

编译器消息: Error C2440 'return': cannot convert from 'const std::shared_ptr<const MyClass>' to 'bool'

标签: c++11shared-ptrimplicit-conversion

解决方案


推荐阅读