gpt4 book ai didi

c++ - libc++ 不允许从 unique_ptr 构造 shared_ptr

转载 作者:搜寻专家 更新时间:2023-10-30 23:52:59 24 4
gpt4 key购买 nike

是否有任何理由导致以下内容在 c++11(或更高版本)中不起作用?

#include <memory>

int main()
{
auto up = std::unique_ptr<int[]>(new int[5]{});
auto sp = std::shared_ptr<int>(std::move(up));
}

我的预期是这将使用列出的第 13 个构造函数 here at cppreference.com .

https://gcc.godbolt.org/ 检查时, visual studio 和 gcc 可以毫无问题地编译它,但是 clang++ - 或者更具体地说是 libc++ (-stdlib=libc++) - 会抛出一个复杂的错误:

<source>:6:13: error: no matching conversion for functional-style cast from 'typename remove_reference<unique_ptr<int const[], default_delete<int const[]> > &>::type' (aka 'std::__1::unique_ptr<int const[], std::__1::default_delete<int const[]> >') to 'std::shared_ptr<const int>'
auto sp = std::shared_ptr<const int>(std::move(up));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[..]c++/v1/memory:3900:23: note: candidate constructor not viable: no known conversion from 'typename remove_reference<unique_ptr<int const[], default_delete<int const[]> > &>::type' (aka 'std::__1::unique_ptr<int const[], std::__1::default_delete<int const[]> >') to 'nullptr_t' for 1st argument
_LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
^
[..]c++/v1/memory:3914:5: note: candidate constructor not viable: no known conversion from 'typename remove_reference<unique_ptr<int const[], default_delete<int const[]> > &>::type' (aka 'std::__1::unique_ptr<int const[], std::__1::default_delete<int const[]> >') to 'const std::__1::shared_ptr<const int>' for 1st argument
shared_ptr(const shared_ptr& __r) _NOEXCEPT;
^
[..]c++/v1/memory:3922:5: note: candidate constructor not viable: no known conversion from 'typename remove_reference<unique_ptr<int const[], default_delete<int const[]> > &>::type' (aka 'std::__1::unique_ptr<int const[], std::__1::default_delete<int const[]> >') to 'std::__1::shared_ptr<const int>' for 1st argument
shared_ptr(shared_ptr&& __r) _NOEXCEPT;
^
[..]c++/v1/memory:3902:18: note: candidate template ignored: could not match '_Yp *' against 'typename remove_reference<unique_ptr<int const[], default_delete<int const[]> > &>::type' (aka 'std::__1::unique_ptr<int const[], std::__1::default_delete<int const[]> >')
explicit shared_ptr(_Yp* __p,
^
[..]c++/v1/memory:3917:9: note: candidate template ignored: could not match 'shared_ptr' against 'unique_ptr'
shared_ptr(const shared_ptr<_Yp>& __r,
^
[..]c++/v1/memory:3923:52: note: candidate template ignored: could not match 'shared_ptr' against 'unique_ptr'
template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
^
[..]c++/v1/memory:3927:34: note: candidate template ignored: could not match 'weak_ptr' against 'unique_ptr'
template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
^
[..]c++/v1/memory:3931:9: note: candidate template ignored: could not match 'auto_ptr' against 'unique_ptr'
shared_ptr(auto_ptr<_Yp>&& __r,
^
[..]c++/v1/memory:3943:24: note: candidate template ignored: disabled by 'enable_if' [with _Yp = int const[], _Dp = std::__1::default_delete<int const[]>]
!is_lvalue_reference<_Dp>::value &&
^
[..]c++/v1/memory:3952:24: note: candidate template ignored: disabled by 'enable_if' [with _Yp = int const[], _Dp = std::__1::default_delete<int const[]>]
is_lvalue_reference<_Dp>::value &&
^
[..]c++/v1/memory:3905:9: note: candidate constructor template not viable: requires at least 2 arguments, but 1 was provided
shared_ptr(_Yp* __p, _Dp __d,
^
[..]c++/v1/memory:3908:9: note: candidate constructor template not viable: requires at least 3 arguments, but 1 was provided
shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
^
[..]c++/v1/memory:3910:26: note: candidate constructor template not viable: requires 2 arguments, but 1 was provided
template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
^
[..]c++/v1/memory:3911:40: note: candidate constructor template not viable: requires 3 arguments, but 1 was provided
template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
^
[..]c++/v1/memory:3912:51: note: candidate constructor template not viable: requires 2 arguments, but 1 was provided
template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
^
[..]c++/v1/memory:3898:23: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
_LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
^
1 error generated.
Compiler exited with result code 1

最佳答案

标准(C++17 之前)仅对第 13 个构造函数说明如下:

This constructor shall not participate in overload resolution unless unique_ptr<Y, D>::pointer is convertible to T*.

但是自std::unique_ptr<int[]>::pointer可转换为 int*代码应该工作。这是 libc++ 中的错误。

在此期间,您可以使用以下内容:

auto sp = std::shared_ptr<int>(up.release(), up.get_deleter());

关于c++ - libc++ 不允许从 unique_ptr<T[]> 构造 shared_ptr<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42615686/

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