weak_ptr 奇怪的复制构造函数

发布于 2024-11-29 10:44:14 字数 945 浏览 6 评论 0原文

以下是weak_ptr的2个构造函数: http://msdn.microsoft.com/en-us/library/bb982126.aspx

weak_ptr(const weak_ptr&);

template<class Other>
weak_ptr(const weak_ptr<Other>&);

实际代码(来自内存):

weak_ptr(const weak_ptr& _Other)
{   // construct weak_ptr object for resource pointed to by _Other
    this->_Resetw(_Other);
}

template<class _Ty2>
weak_ptr(const weak_ptr<_Ty2>& _Other,
         typename enable_if<is_convertible<_Ty2 *, _Ty *>::value,
         void *>::type * = 0)
{   // construct weak_ptr object for resource pointed to by _Other
    this->_Resetw(_Other);
}

Q1:为什么顶部复制构造函数在那里?看起来底部的一个占所有情况(包括顶部的一个)。它甚至被调用吗?如果他们不包括它,底部的会取代它的位置吗?

Q2:底部(模板化)构造函数的第二个参数发生了什么?我想我理解 SFINAE 方面,但我不明白为什么在 ::type 之后有一个额外的 *

the following are 2 of weak_ptr's constructors:
http://msdn.microsoft.com/en-us/library/bb982126.aspx

weak_ptr(const weak_ptr&);

template<class Other>
weak_ptr(const weak_ptr<Other>&);

actual code (from memory):

weak_ptr(const weak_ptr& _Other)
{   // construct weak_ptr object for resource pointed to by _Other
    this->_Resetw(_Other);
}

template<class _Ty2>
weak_ptr(const weak_ptr<_Ty2>& _Other,
         typename enable_if<is_convertible<_Ty2 *, _Ty *>::value,
         void *>::type * = 0)
{   // construct weak_ptr object for resource pointed to by _Other
    this->_Resetw(_Other);
}

Q1: Why is the top copy constructor even there? It looks like the bottom one accounts for every case (including the top one). Does it even get called? and if they didn't include it would the bottom one take it's place?

Q2: What's going on with the second argument of the bottom (templated) constructor. I think I understand the SFINAE aspect, but I don't understand why there is an extra * after ::type

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

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

发布评论

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

评论(2

我们的影子 2024-12-06 10:44:14

Q1) 如果您不编写复制构造函数,编译器将为您生成一个复制构造函数,这不是您想要的。模板化转换构造函数不算在内。

Q2) 请记住,shared_ptr 就像 T*,必须在指针级别检查可转换性。如果 T* 可转换为 U* 那么您应该能够将一个分配给另一个。想想指向基点的指针。 [抱歉,这不是您要求的。] 最终参数类型只需要存在,但我们也不想指定参数本身。构造类型的通用方法是指针,我们也可以为其提供默认参数。简而言之,我们需要使函数依赖于可能存在或可能不存在的类型,但实际上不需要用户知道这一点。

Q1) If you don't write a copy constructor, the compiler will generate one for you, which wouldn't be what you want. Templated conversion constructors don't count.

Q2) Remember that shared_ptr<T> is like a T*, convertibility must be checked at the level of pointers. If T* is convertible to U* then you should be able to assign one to the other. Think of pointers-to-base. [Sorry, that wasn't what you asked.] The final argument type just needs to exist, but also we don't want to have to specify the argument itself. A universal way of making up a type for which we can also provide a default argument is a pointer. In short, we need to make the function depend on a type that may or may not exist, but without actually requiring the user to know about this.

贪了杯 2024-12-06 10:44:14

关于问题 1:模板化构造函数永远不是“复制构造函数”,即使它能够复制。如果没有用户定义的“复制构造函数”,那么编译器将根据需要生成一个。

回复 Q2:第二个参数,默认为 0 的指针,只是为了有地方放置 enable_if。你可以在 Boost 文档中找到更多相关信息(如果我没记错的话)。

干杯&呵呵,

Re Q1: a templated constructor is never a "copy constructor", even if it manages to copy. if there is no user-defined "copy constructor", then the compiler will generate one as needed.

Re Q2: the second argument, a pointer defaulted to 0, is just to have a place to put the enable_if. you can find more about that (if i recall correctly) in the Boost documentation.

Cheers & hth.,

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