gpt4 book ai didi

c++ - 为什么 auto_ptr 似乎违反了 Visual C++ 上的私有(private)继承?

转载 作者:IT老高 更新时间:2023-10-28 22:41:49 26 4
gpt4 key购买 nike

背景信息:这是在 Visual Studio 2008 上检测到的,并在 Visual Studio 2013 上再次确认。G++ 对代码大喊大叫,而 Visual 默默地接受了私有(private)继承漏洞。

所以,在 Visual C++ 上,我们有以下代码:

class Base {};
class Derived : Base {}; // inherits privately. Adding explicitly the
// keyword private changes nothing

int main()
{
std::auto_ptr<Base>(new Derived) ; // compiles, which is NOT EXPECTED
std::auto_ptr<Base> p(new Derived) ; // Does not compile, which is expected
}

为什么第一个(临时)auto_ptr 会编译?我在调试时进入了它,它完全按照公共(public)继承应该做的事情(调用正确的构造函数等)

想知道问题是否与 auto_ptr 实现有关(我们永远不知道...),我减少了这个独立代码的问题:

class Base {};
class Derived : Base {};

template <typename T>
class Ptr
{
T * m_p;

public :
Ptr(T * p_p)
: m_p(p_p)
{
}
} ;

int main()
{
Ptr<Base>(new Derived) ; // compiles, which is NOT EXPECTED
Ptr<Base> p(new Derived) ; // Does not compile, which is expected
}

再次,我希望代码不会编译,因为 Derived 是从 Base 私有(private)继承的。

但是当我们创建一个临时的,它就起作用了。

我们不能把它归咎于 std::auto_ptr。

我错过了标准中的某些内容(98 或 11 或 14),还是这是一个错误?

最佳答案

Derived* -to- Base*即使继承是私有(private)的,在 C 风格和函数式强制转换中也允许转换。不,这并不意味着 reinterpret_cast在那种情况下。

这是标准不允许的,但它非常看起来好像是允许的,所以这是一个微妙的错误。

5.2.3 Explicit type conversion (functional notation) [expr.type.conv]

1 [...] If the expression list is a single expression, the type conversion expression is equivalent (in definedness, and if defined in meaning) to the corresponding cast expression (5.4). [...]

5.4 Explicit type conversion (cast notation) [expr.cast]

4 The conversions performed by

  • a const_cast (5.2.11),
  • a static_cast (5.2.9),
  • a static_cast followed by a const_cast,
  • a reinterpret_cast (5.2.10), or
  • a reinterpret_cast followed by a const_cast,

can be performed using the cast notation of explicit type conversion. The same semantic restrictions and behaviors apply, with the exception that in performing a static_cast in the following situations the conversion is valid even if the base class is inaccessible:

  • a pointer to an object of derived class type or an lvalue or rvalue of derived class type may be explicitly converted to a pointer or reference to an unambiguous base class type, respectively;
  • [...]

在您遇到的情况下,编译器将其解释为 static_cast来自 Derived*auto_ptr<Base> , 并且在那个 static_cast ,指向派生类类型对象的指针被转换为明确基类类型的指针。所以看起来标准允许它。

但是,从 Derived* 的转换至Base*是隐式的,它只是碰巧作为显式不同转换的一部分执行。所以最后,不,标准真的不允许。

您可能希望将此报告为错误。来自 Csq的评论,我们得知有一个related report , 其中一个显式的 static_cast也允许这种转换,但它并不完全相同。在这种情况下,从 Derived* 的转换至Base*是显式的,但在这里是隐式的,Visual C++ 通常会在隐式转换中拒绝它。

请注意,在使用多个表达式的函数转换中,这种误解是不可能的:编译器正确拒绝以下内容:

class Base { };
class Derived : Base { };

template <typename T>
class Ptr {
public:
Ptr(T *a, T *b) { }
};

int main() {
Ptr<Base>(new Derived, new Derived);
// error C2243: 'type cast' : conversion from 'Derived *' to 'Base *' exists, but is inaccessible
}

关于c++ - 为什么 auto_ptr 似乎违反了 Visual C++ 上的私有(private)继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24303735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com