在以下继承结构中,IFace
是Face
的私有基类:
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
分享其引用次数?
答案 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
是使用内置的编译器实现的,因此归结为编译器错误。