使用 make_shared<U[]>( std::size_t N )` 时出错

Sui*_*anP 1 c++ smart-pointers shared-ptr c++20

我正在尝试实现一个固定大小的多维数组,其大小在运行时确定。make_shared与( )的 (2) 重载template<class T> shared_ptr<T> make_shared(std::size_t N) // T is U[]。但是,我面临编译错误(日志如下)。shared如果我将s 更改为其对应项,则不会出现该错误unique。我的问题是,

\n
    \n
  • 这个错误是关于什么的?
  • \n
  • 为什么unique有效?
  • \n
  • 有更好的方法来实现这种运行时固定的多维数组容器吗?
  • \n
\n

最小工作示例:

\n
#include <memory>\n#include <iostream>\nint main() {\n    typedef int cell_t;\n    std::size_t x, y;\n    std::cin >> y >> x;\n    auto layout = std::make_shared<std::shared_ptr<cell_t[]>[]>(y);\n    for (std::size_t i = 0; i < y; i += 1) {\n        layout[i] = std::make_shared<cell_t[]>(x);\n    }\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

错误消息如下g++-10(为简洁起见,省略了注释)

\n
In file included from /usr/include/c++/10/ext/alloc_traits.h:34,\n                 from /usr/include/c++/10/bits/stl_uninitialized.h:67,\n                 from /usr/include/c++/10/memory:66,\n                 from test_shared.cpp:1:\n/usr/include/c++/10/bits/alloc_traits.h: In instantiation of \xe2\x80\x98static constexpr void std::allocator_traits<std::allocator<_Up> >::construct(std::allocator_traits<std::allocator<_Up> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::shared_ptr<int []>; _Args = {long unsigned int&}; _Tp = std::shared_ptr<int []> []; std::allocator_traits<std::allocator<_Up> >::allocator_type = std::allocator<std::shared_ptr<int []> []>]\xe2\x80\x99:\n/usr/include/c++/10/bits/shared_ptr_base.h:551:39:   required from \xe2\x80\x98std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {long unsigned int&}; _Tp = std::shared_ptr<int []>; _Alloc = std::allocator<std::shared_ptr<int []> []>; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr_base.h:682:16:   required from \xe2\x80\x98std::__shared_count<_Lp>::__shared_count(_Tp*&, std::_Sp_alloc_shared_tag<_Alloc>, _Args&& ...) [with _Tp = std::shared_ptr<int []>; _Alloc = std::allocator<std::shared_ptr<int []> []>; _Args = {long unsigned int&}; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr_base.h:1371:71:   required from \xe2\x80\x98std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::allocator<std::shared_ptr<int []> []>; _Args = {long unsigned int&}; _Tp = std::shared_ptr<int []> []; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr.h:408:59:   required from \xe2\x80\x98std::shared_ptr<_Tp>::shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::allocator<std::shared_ptr<int []> []>; _Args = {long unsigned int&}; _Tp = std::shared_ptr<int []> []]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr.h:859:14:   required from \xe2\x80\x98std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = std::shared_ptr<int []> []; _Alloc = std::allocator<std::shared_ptr<int []> []>; _Args = {long unsigned int&}]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr.h:875:39:   required from \xe2\x80\x98std::shared_ptr<_Tp> std::make_shared(_Args&& ...) [with _Tp = std::shared_ptr<int []> []; _Args = {long unsigned int&}]\xe2\x80\x99\ntest_shared.cpp:7:63:   required from here\n/usr/include/c++/10/bits/alloc_traits.h:514:21: error: no matching function for call to \xe2\x80\x98construct_at(std::shared_ptr<int []>*&, long unsigned int&)\xe2\x80\x99\n  514 |    std::construct_at(__p, std::forward<_Args>(__args)...);\n      |    ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nIn file included from /usr/include/c++/10/memory:65,\n                 from test_shared.cpp:1:\n/usr/include/c++/10/bits/stl_construct.h: In substitution of \xe2\x80\x98template<class _Tp, class ... _Args> constexpr decltype (::new(void*(0)) _Tp) std::construct_at(_Tp*, _Args&& ...) [with _Tp = std::shared_ptr<int []>; _Args = {long unsigned int&}]\xe2\x80\x99:\n/usr/include/c++/10/bits/alloc_traits.h:514:21:   required from \xe2\x80\x98static constexpr void std::allocator_traits<std::allocator<_Up> >::construct(std::allocator_traits<std::allocator<_Up> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::shared_ptr<int []>; _Args = {long unsigned int&}; _Tp = std::shared_ptr<int []> []; std::allocator_traits<std::allocator<_Up> >::allocator_type = std::allocator<std::shared_ptr<int []> []>]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr_base.h:551:39:   required from \xe2\x80\x98std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {long unsigned int&}; _Tp = std::shared_ptr<int []>; _Alloc = std::allocator<std::shared_ptr<int []> []>; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr_base.h:682:16:   required from \xe2\x80\x98std::__shared_count<_Lp>::__shared_count(_Tp*&, std::_Sp_alloc_shared_tag<_Alloc>, _Args&& ...) [with _Tp = std::shared_ptr<int []>; _Alloc = std::allocator<std::shared_ptr<int []> []>; _Args = {long unsigned int&}; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr_base.h:1371:71:   required from \xe2\x80\x98std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::allocator<std::shared_ptr<int []> []>; _Args = {long unsigned int&}; _Tp = std::shared_ptr<int []> []; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr.h:408:59:   required from \xe2\x80\x98std::shared_ptr<_Tp>::shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::allocator<std::shared_ptr<int []> []>; _Args = {long unsigned int&}; _Tp = std::shared_ptr<int []> []]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr.h:859:14:   required from \xe2\x80\x98std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = std::shared_ptr<int []> []; _Alloc = std::allocator<std::shared_ptr<int []> []>; _Args = {long unsigned int&}]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr.h:875:39:   required from \xe2\x80\x98std::shared_ptr<_Tp> std::make_shared(_Args&& ...) [with _Tp = std::shared_ptr<int []> []; _Args = {long unsigned int&}]\xe2\x80\x99\ntest_shared.cpp:7:63:   required from here\n/usr/include/c++/10/bits/stl_construct.h:96:17: error: no matching function for call to \xe2\x80\x98std::shared_ptr<int []>::shared_ptr(long unsigned int&)\xe2\x80\x99\n   96 |     -> decltype(::new((void*)0) _Tp(std::declval<_Args>()...))\n      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n
Run Code Online (Sandbox Code Playgroud)\n

g++-10 -v输出:

\n
Using built-in specs.\nCOLLECT_GCC=g++-10\nCOLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/10/lto-wrapper\nOFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa:hsa\nOFFLOAD_TARGET_DEFAULT=1\nTarget: x86_64-linux-gnu\nConfigured with: ../src/configure -v --with-pkgversion=\'Ubuntu 10.2.0-5ubuntu1~20.04\' --with-bugurl=file:///usr/share/doc/gcc-10/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-10 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-10-WJNXnb/gcc-10-10.2.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-10-WJNXnb/gcc-10-10.2.0/debian/tmp-gcn/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu\nThread model: posix\nSupported LTO compression algorithms: zlib zstd\ngcc version 10.2.0 (Ubuntu 10.2.0-5ubuntu1~20.04) \n
Run Code Online (Sandbox Code Playgroud)\n

并来自clang

\n
In file included from test_shared.cpp:1:\nIn file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/memory:64:\nIn file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/allocator.h:46:\nIn file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10/bits/c++allocator.h:33:\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/ext/new_allocator.h:150:23: error: no matching constructor for initialization of \'std::shared_ptr<int []>\'\n        { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }\n                             ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/alloc_traits.h:512:8: note: in instantiation of function template specialization \'__gnu_cxx::new_allocator<std::shared_ptr<int []> []>::construct<std::shared_ptr<int []>, unsigned long &>\' requested here\n          __a.construct(__p, std::forward<_Args>(__args)...);\n              ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr_base.h:551:30: note: in instantiation of function template specialization \'std::allocator_traits<std::allocator<std::shared_ptr<int []> []> >::construct<std::shared_ptr<int []>, unsigned long &>\' requested here\n          allocator_traits<_Alloc>::construct(__a, _M_ptr(),\n                                    ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr_base.h:683:6: note: in instantiation of function template specialization \'std::_Sp_counted_ptr_inplace<std::shared_ptr<int []>, std::allocator<std::shared_ptr<int []> []>, __gnu_cxx::_S_atomic>::_Sp_counted_ptr_inplace<unsigned long &>\' requested here\n            _Sp_cp_type(__a._M_a, std::forward<_Args>(__args)...);\n            ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr_base.h:1371:14: note: in instantiation of function template specialization \'std::__shared_count<__gnu_cxx::_S_atomic>::__shared_count<std::shared_ptr<int []>, std::allocator<std::shared_ptr<int []> []>, unsigned long &>\' requested here\n        : _M_ptr(), _M_refcount(_M_ptr, __tag, std::forward<_Args>(__args)...)\n                    ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:408:4: note: in instantiation of function template specialization \'std::__shared_ptr<std::shared_ptr<int []> [], __gnu_cxx::_S_atomic>::__shared_ptr<std::allocator<std::shared_ptr<int []> []>, unsigned long &>\' requested here\n        : __shared_ptr<_Tp>(__tag, std::forward<_Args>(__args)...)\n          ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:859:14: note: in instantiation of function template specialization \'std::shared_ptr<std::shared_ptr<int []> []>::shared_ptr<std::allocator<std::shared_ptr<int []> []>, unsigned long &>\' requested here\n      return shared_ptr<_Tp>(_Sp_alloc_shared_tag<_Alloc>{__a},\n             ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:875:19: note: in instantiation of function template specialization \'std::allocate_shared<std::shared_ptr<int []> [], std::allocator<std::shared_ptr<int []> []>, unsigned long &>\' requested here\n      return std::allocate_shared<_Tp>(std::allocator<_Tp_nc>(),\n                  ^\ntest_shared.cpp:7:21: note: in instantiation of function template specialization \'std::make_shared<std::shared_ptr<int []> [], unsigned long &>\' requested here\n        auto layout = std::make_shared<std::shared_ptr<cell_t[]>[]>(y);\n                           ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:149:7: note: candidate constructor not viable: no known conversion from \'unsigned long\' to \'const std::shared_ptr<int []>\' for 1st argument\n      shared_ptr(const shared_ptr&) noexcept = default; ///< Copy constructor\n      ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:303:7: note: candidate constructor not viable: no known conversion from \'unsigned long\' to \'std::shared_ptr<int []>\' for 1st argument\n      shared_ptr(shared_ptr&& __r) noexcept\n      ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:356:17: note: candidate constructor not viable: no known conversion from \'unsigned long\' to \'std::nullptr_t\' (aka \'nullptr_t\') for 1st argument\n      constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { }\n                ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:159:2: note: candidate template ignored: could not match \'_Yp *\' against \'unsigned long\'\n        shared_ptr(_Yp* __p) : __shared_ptr<_Tp>(__p) { }\n        ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:295:2: note: candidate template ignored: could not match \'shared_ptr<type-parameter-0-0>\' against \'unsigned long\'\n        shared_ptr(const shared_ptr<_Yp>& __r) noexcept\n        ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:312:2: note: candidate template ignored: could not match \'shared_ptr<type-parameter-0-0>\' against \'unsigned long\'\n        shared_ptr(shared_ptr<_Yp>&& __r) noexcept\n        ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:324:11: note: candidate template ignored: could not match \'weak_ptr<type-parameter-0-0>\' against \'unsigned long\'\n        explicit shared_ptr(const weak_ptr<_Yp>& __r)\n                 ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:331:2: note: candidate template ignored: could not match \'auto_ptr<type-parameter-0-0>\' against \'unsigned long\'\n        shared_ptr(auto_ptr<_Yp>&& __r);\n        ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:339:2: note: candidate template ignored: could not match \'unique_ptr<type-parameter-0-0, type-parameter-0-1>\' against \'unsigned long\'\n        shared_ptr(unique_ptr<_Yp, _Del>&& __r)\n        ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:348:2: note: candidate template ignored: could not match \'unique_ptr<type-parameter-0-0, type-parameter-0-1>\' against \'unsigned long\'\n        shared_ptr(unique_ptr<_Yp, _Del>&& __r)\n        ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:407:2: note: candidate template ignored: could not match \'_Sp_alloc_shared_tag<type-parameter-0-0>\' against \'unsigned long\'\n        shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)\n        ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:147:17: note: candidate constructor not viable: requires 0 arguments, but 1 was provided\n      constexpr shared_ptr() noexcept : __shared_ptr<_Tp>() { }\n                ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:176:2: note: candidate constructor template not viable: requires 2 arguments, but 1 was provided\n        shared_ptr(_Yp* __p, _Deleter __d)\n        ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:193:2: note: candidate constructor template not viable: requires 2 arguments, but 1 was provided\n        shared_ptr(nullptr_t __p, _Deleter __d)\n        ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:256:2: note: candidate constructor template not viable: requires 2 arguments, but 1 was provided\n        shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) noexcept\n        ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:416:7: note: candidate constructor not viable: requires 2 arguments, but 1 was provided\n      shared_ptr(const weak_ptr<_Tp>& __r, std::nothrow_t)\n      ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:213:2: note: candidate constructor template not viable: requires 3 arguments, but 1 was provided\n        shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)\n        ^\n/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/shared_ptr.h:232:2: note: candidate constructor template not viable: requires 3 arguments, but 1 was provided\n        shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)\n        ^\n1 error generated.\n
Run Code Online (Sandbox Code Playgroud)\n

clang -v输出:

\n
clang version 10.0.0-4ubuntu1 \nTarget: x86_64-pc-linux-gnu\nThread model: posix\nInstalledDir: /usr/bin\nFound candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10\nFound candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8\nFound candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9\nFound candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10\nFound candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8\nFound candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9\nSelected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10\nCandidate multilib: .;@m64\nSelected multilib: .;@m64\n
Run Code Online (Sandbox Code Playgroud)\n

原始代码,如果有帮助的话,

\n
In file included from /usr/include/c++/10/ext/alloc_traits.h:34,\n                 from /usr/include/c++/10/bits/stl_uninitialized.h:67,\n                 from /usr/include/c++/10/memory:66,\n                 from test_shared.cpp:1:\n/usr/include/c++/10/bits/alloc_traits.h: In instantiation of \xe2\x80\x98static constexpr void std::allocator_traits<std::allocator<_Up> >::construct(std::allocator_traits<std::allocator<_Up> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::shared_ptr<int []>; _Args = {long unsigned int&}; _Tp = std::shared_ptr<int []> []; std::allocator_traits<std::allocator<_Up> >::allocator_type = std::allocator<std::shared_ptr<int []> []>]\xe2\x80\x99:\n/usr/include/c++/10/bits/shared_ptr_base.h:551:39:   required from \xe2\x80\x98std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {long unsigned int&}; _Tp = std::shared_ptr<int []>; _Alloc = std::allocator<std::shared_ptr<int []> []>; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr_base.h:682:16:   required from \xe2\x80\x98std::__shared_count<_Lp>::__shared_count(_Tp*&, std::_Sp_alloc_shared_tag<_Alloc>, _Args&& ...) [with _Tp = std::shared_ptr<int []>; _Alloc = std::allocator<std::shared_ptr<int []> []>; _Args = {long unsigned int&}; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr_base.h:1371:71:   required from \xe2\x80\x98std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::allocator<std::shared_ptr<int []> []>; _Args = {long unsigned int&}; _Tp = std::shared_ptr<int []> []; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr.h:408:59:   required from \xe2\x80\x98std::shared_ptr<_Tp>::shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::allocator<std::shared_ptr<int []> []>; _Args = {long unsigned int&}; _Tp = std::shared_ptr<int []> []]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr.h:859:14:   required from \xe2\x80\x98std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = std::shared_ptr<int []> []; _Alloc = std::allocator<std::shared_ptr<int []> []>; _Args = {long unsigned int&}]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr.h:875:39:   required from \xe2\x80\x98std::shared_ptr<_Tp> std::make_shared(_Args&& ...) [with _Tp = std::shared_ptr<int []> []; _Args = {long unsigned int&}]\xe2\x80\x99\ntest_shared.cpp:7:63:   required from here\n/usr/include/c++/10/bits/alloc_traits.h:514:21: error: no matching function for call to \xe2\x80\x98construct_at(std::shared_ptr<int []>*&, long unsigned int&)\xe2\x80\x99\n  514 |    std::construct_at(__p, std::forward<_Args>(__args)...);\n      |    ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nIn file included from /usr/include/c++/10/memory:65,\n                 from test_shared.cpp:1:\n/usr/include/c++/10/bits/stl_construct.h: In substitution of \xe2\x80\x98template<class _Tp, class ... _Args> constexpr decltype (::new(void*(0)) _Tp) std::construct_at(_Tp*, _Args&& ...) [with _Tp = std::shared_ptr<int []>; _Args = {long unsigned int&}]\xe2\x80\x99:\n/usr/include/c++/10/bits/alloc_traits.h:514:21:   required from \xe2\x80\x98static constexpr void std::allocator_traits<std::allocator<_Up> >::construct(std::allocator_traits<std::allocator<_Up> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::shared_ptr<int []>; _Args = {long unsigned int&}; _Tp = std::shared_ptr<int []> []; std::allocator_traits<std::allocator<_Up> >::allocator_type = std::allocator<std::shared_ptr<int []> []>]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr_base.h:551:39:   required from \xe2\x80\x98std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {long unsigned int&}; _Tp = std::shared_ptr<int []>; _Alloc = std::allocator<std::shared_ptr<int []> []>; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr_base.h:682:16:   required from \xe2\x80\x98std::__shared_count<_Lp>::__shared_count(_Tp*&, std::_Sp_alloc_shared_tag<_Alloc>, _Args&& ...) [with _Tp = std::shared_ptr<int []>; _Alloc = std::allocator<std::shared_ptr<int []> []>; _Args = {long unsigned int&}; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr_base.h:1371:71:   required from \xe2\x80\x98std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::allocator<std::shared_ptr<int []> []>; _Args = {long unsigned int&}; _Tp = std::shared_ptr<int []> []; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr.h:408:59:   required from \xe2\x80\x98std::shared_ptr<_Tp>::shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::allocator<std::shared_ptr<int []> []>; _Args = {long unsigned int&}; _Tp = std::shared_ptr<int []> []]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr.h:859:14:   required from \xe2\x80\x98std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = std::shared_ptr<int []> []; _Alloc = std::allocator<std::shared_ptr<int []> []>; _Args = {long unsigned int&}]\xe2\x80\x99\n/usr/include/c++/10/bits/shared_ptr.h:875:39:   required from \xe2\x80\x98std::shared_ptr<_Tp> std::make_shared(_Args&& ...) [with _Tp = std::shared_ptr<int []> []; _Args = {long unsigned int&}]\xe2\x80\x99\ntest_shared.cpp:7:63:   required from here\n/usr/include/c++/10/bits/stl_construct.h:96:17: error: no matching function for call to \xe2\x80\x98std::shared_ptr<int []>::shared_ptr(long unsigned int&)\xe2\x80\x99\n   96 |     -> decltype(::new((void*)0) _Tp(std::declval<_Args>()...))\n      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n
Run Code Online (Sandbox Code Playgroud)\n

小智 6

对于你的第一个问题“这个错误是什么?”:

GCC libstdc++ 和 Clang libc++ 尚不支持std::make_shared()c++20 中引入的“扩展以支持数组”。因此,这些编译器将尝试使用template< class T, class... Args > shared_ptr<T> make_shared( Args&&... args );,它尝试转发您的参数(在本例中为 a cell_t= std::size_t)来构造std::shared_ptr<cell_t[]>[]. 这是做不到的,所以他们抱怨。

您可以在此处检查编译器兼容性:Compiler support for C++20