在这种情况下 unique_ptr 的行为应该是什么?

发布于 2024-12-21 04:11:57 字数 328 浏览 4 评论 0原文

假设我有以下内容:

std::unique_ptr<A> pA;
pA(new A);

在这个复杂的示例中,pA(new A); 的行为应该是什么?

据我所知,在 MSVC2010 中,default_delete 中的 void operator()(T*) const; 在 new 返回后立即调用,并立即删除指针。而 g++(4.7.0) 给了我 no match for call (std::unique_ptr)(A*) 错误。

Say I have the following:

std::unique_ptr<A> pA;
pA(new A);

In this convoluted example, what should the behavior of pA(new A); be?

As far as I can tell, in MSVC2010, void operator()(T*) const; from default_delete is called right after new returns and deletes the pointer right away. Whereas g++(4.7.0) gave me no match for call (std::unique_ptr<A>)(A*) error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独自←快乐 2024-12-28 04:11:57

该代码不应编译。 std::unique_ptr 不会重载 operator()

Visual C++ 2011 开发者预览版正确地拒绝了该代码。 Visual C++ 2010 仅接受代码,因为 std::unique_ptr 实现中存在错误

The code should not compile. std::unique_ptr does not overload operator().

The Visual C++ 2011 Developer Preview rightly rejects the code. Visual C++ 2010 only accepts the code due to a bug in its std::unique_ptr implementation.

拥抱影子 2024-12-28 04:11:57

MSVC 对unique_ptr 采用无状态删除器优化,即它利用空基类优化并仅从删除器继承。不幸的是,继承是public,这就是为什么您可以访问default_delete函子的重载operator()

MSVC employs the state-less deleter optimization for unique_ptr, i.e. it exploits the empty-base-class-optimization and just inherits from the deleter. Unfortunately, the inheritance is public, which is why you have access to the overloaded operator() of the default_delete functor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文