Ошибка сборки «Нет соответствующего конструктора для инициализации» при инициализацииshare_ptr из unique_ptrC++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 Ошибка сборки «Нет соответствующего конструктора для инициализации» при инициализацииshare_ptr из unique_ptr

Сообщение Anonymous »

Во-первых, я прочитал этот ответ, чтобы понять, почему иногда нам нужно преобразовать unique_ptr вshared_ptr. Затем я следую инструкциям конструктора преобразования cppreference Shared_ptr.

В C++11 и C++14 допустимо создать стандартный стандарт: :shared_ptr из std::unique_ptr:

Код: Выделить всё

std::unique_ptr arr(new int[1]);
std::shared_ptr ptr(std::move(arr));
Посколькуshared_ptr получает свой элемент удаления (объект std::default_delete) из std::unique_ptr, массив будет правильно освобожден .
Это больше не разрешено в C++17. Вместо этого следует использовать форму массива std::shared_ptr.

Итак, я пробую это на Ubuntu и Windows соответственно. Следующий код можно собрать PASS в Ubuntu с использованием g++ v11.4.0

Код: Выделить всё

g++ -std=c++17 ./test.cpp -o test_cpp

Код: Выделить всё

#include 

int main()
{
std::unique_ptr arr(new int[1]);
std::shared_ptr ptr(std::move(arr));

return 0;
}
С другой стороны, в Windows 11 при использовании llvm-mingw g++ v12.0.0 отображаются следующие сообщения об ошибках:

Код: Выделить всё

.\test.cpp:6:28: error: no matching constructor for initialization of 'std::shared_ptr'
std::shared_ptr ptr(std::move(arr));
^   ~~~~~~~~~~~~~~
C:\local\llvm-mingw-20210423-msvcrt-x86_64\include\c++\v1\memory:2711:23: note: candidate constructor not viable: no known conversion from 'typename remove_reference::type'
(aka 'std::unique_ptr') to 'std::nullptr_t' (aka 'nullptr_t') for 1st argument
_LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
^
C:\local\llvm-mingw-20210423-msvcrt-x86_64\include\c++\v1\memory:2725:5: note: candidate constructor not viable: no known conversion from 'typename remove_reference::type'
(aka 'std::unique_ptr') to 'const std::shared_ptr' for 1st argument
shared_ptr(const shared_ptr& __r) _NOEXCEPT;
^
C:\local\llvm-mingw-20210423-msvcrt-x86_64\include\c++\v1\memory:2732:5: note: candidate constructor not viable: no known conversion from 'typename remove_reference::type'
(aka 'std::unique_ptr') to 'std::shared_ptr' for 1st argument
shared_ptr(shared_ptr&& __r) _NOEXCEPT;
^
C:\local\llvm-mingw-20210423-msvcrt-x86_64\include\c++\v1\memory:2713:18: note: candidate template ignored: could not match '_Yp *' against 'typename remove_reference::type'
(aka 'std::unique_ptr')
explicit shared_ptr(_Yp* __p,
^
C:\local\llvm-mingw-20210423-msvcrt-x86_64\include\c++\v1\memory:2728:9: note: candidate template ignored: could not match 'shared_ptr' against 'unique_ptr'
shared_ptr(const shared_ptr& __r,
^
C:\local\llvm-mingw-20210423-msvcrt-x86_64\include\c++\v1\memory:2733:52: note: candidate template ignored: could not match 'shared_ptr' against 'unique_ptr'
template _LIBCPP_INLINE_VISIBILITY  shared_ptr(shared_ptr&& __r,
^
C:\local\llvm-mingw-20210423-msvcrt-x86_64\include\c++\v1\memory:2736:34: note: candidate template ignored: could not match 'weak_ptr' against 'unique_ptr'
template explicit shared_ptr(const weak_ptr& __r,
^
C:\local\llvm-mingw-20210423-msvcrt-x86_64\include\c++\v1\memory:2744:9: note: candidate template ignored: requirement '!is_array::value' was not satisfied
[with _Yp = int [], _Dp = std::default_delete]
shared_ptr(unique_ptr&&,
^
C:\local\llvm-mingw-20210423-msvcrt-x86_64\include\c++\v1\memory:2753:9: note: candidate template ignored: requirement 'is_lvalue_reference::value' was not satisfied
[with _Yp = int [], _Dp = std::default_delete]
shared_ptr(unique_ptr&&,
^
C:\local\llvm-mingw-20210423-msvcrt-x86_64\include\c++\v1\memory:2709:23: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
_LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
^
C:\local\llvm-mingw-20210423-msvcrt-x86_64\include\c++\v1\memory:2721:26: note: candidate constructor template not viable: requires 2 arguments, but 1 was provided
template  shared_ptr(nullptr_t __p, _Dp __d);
^
C:\local\llvm-mingw-20210423-msvcrt-x86_64\include\c++\v1\memory:2723:51: note: candidate constructor template not viable: requires 2 arguments, but 1 was provided
template _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr&  __r, element_type* __p) _NOEXCEPT;
^
C:\local\llvm-mingw-20210423-msvcrt-x86_64\include\c++\v1\memory:2716:9: note: candidate constructor template not viable: requires at least 2 arguments, but 1 was provided
shared_ptr(_Yp* __p, _Dp __d,
^
C:\local\llvm-mingw-20210423-msvcrt-x86_64\include\c++\v1\memory:2722:40: note: candidate constructor template not viable: requires 3 arguments, but 1 was provided
template  shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
^
C:\local\llvm-mingw-20210423-msvcrt-x86_64\include\c++\v1\memory:2719:9: note: candidate constructor template not viable: requires at least 3 arguments, but 1 was provided
shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
^
1 error generated.
Итак, мой вопрос:
  • Что-то не так в моем тестовом коде?
  • Почему в этих двух средах результаты различаются?
  • Как заставить работать llvm-mingw в Windows?


Подробнее здесь: https://stackoverflow.com/questions/786 ... alize-shar
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C++»