shared_ptr如何转换为私有基类?

时间:2014-08-02 23:27:27

标签: c++ pointers inheritance c++11 type-conversion

在以下继承结构中,IFaceFace的私有基类:

class IFace{};

class Face : /*private*/ IFace{};

虽然从原始Face*IFace*指针的转换当然失败,但shared_ptr<Face>仍然可以转换为shared_ptr<IFace>

shared_ptr<Face> s_face = make_shared<Face>();
shared_ptr<IFace> s_iface = s_face; //works

Face* p_face = new Face();
IFace* p_iface = p_face; //compiler complains (righteously) 
                         //about the base class being inaccessible

shared_ptr在这里玩什么魔术?

标准是否涵盖此行为,还是特定于我的实现(MSVC12)?

新创建的s_iface是否与s_face分享其引用次数?

1 个答案:

答案 0 :(得分:4)

如果指针不可隐式转换,则标准要求转换构造函数为SFINAE&out;(标准为#34;不应参与重载决策&#34;)。这看起来像MSVC的std::is_convertible实现中的错误:

#include <type_traits>
class IFace {};

class Face : /*private*/ IFace{};

int main() {
    static_assert(std::is_convertible<Face *, IFace *>::value, "Face * is not convertible to IFace *");
    static_assert(!std::is_convertible<Face *, IFace *>::value, "Face * is convertible to IFace *");
}

使用MSVC12给我error C2338: Face * is convertible to IFace *

clang和g ++在第一个static_assert正确失败并报告Face * is not convertible to IFace *

查看MSVC的<type_traits>标头,is_convertible是使用内置的编译器实现的,因此归结为编译器错误。