Member 14068785 Ответов: 1

Проблема компиляции shared_ptr с массивами c++17 mac OS


Уважаемые эксперты,
У меня есть простой код, который использует shared_ptr с массивами.
Строка кода, которая дает проблему, такова:
std::shared_ptr<double[]> myArray(new double[10]);

Я использую Mac OS 10.14.1 и Xcode 10.1 из командной строки:
c++ -std=c++17 -o sharedPtr sharedPtr.cpp

По-видимому, я думаю, что это должно сработать.
Видите ли вы какие-либо проблемы совместимости с компилятором Mac Xcode C++?
Большое спасибо.
- Мауро.

Что я уже пробовал:

sharedPtr.cpp:70:29: error: no matching constructor for initialization of 'std::shared_ptr<double []>'
  std::shared_ptr<double[]> myArray(new double[10]);
                            ^       ~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3703:23: note: candidate
      constructor not viable: no known conversion from 'double *' to 'std::nullptr_t' (aka 'nullptr_t') for 1st argument
    _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
                      ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3717:5: note: candidate
      constructor not viable: no known conversion from 'double *' to 'const std::__1::shared_ptr<double []>' for 1st argument
    shared_ptr(const shared_ptr& __r) _NOEXCEPT;
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3725:5: note: candidate
      constructor not viable: no known conversion from 'double *' to 'std::__1::shared_ptr<double []>' for 1st argument
    shared_ptr(shared_ptr&& __r) _NOEXCEPT;
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3705:18: note: candidate
      template ignored: requirement 'is_convertible<double *, element_type *>::value' was not satisfied [with _Yp = double]
        explicit shared_ptr(_Yp* __p,
                 ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3720:9: note: candidate template
      ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'double *'
        shared_ptr(const shared_ptr<_Yp>& __r,
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3726:52: note: candidate
      template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'double *'
    template<class _Yp> _LIBCPP_INLINE_VISIBILITY  shared_ptr(shared_ptr<_Yp>&& __r,
                                                   ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3730:34: note: candidate
      template ignored: could not match 'weak_ptr<type-parameter-0-0>' against 'double *'
    template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
                                 ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3745:9: note: candidate template
      ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'double *'
        shared_ptr(unique_ptr<_Yp, _Dp>&&,
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3754:9: note: candidate template
      ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'double *'
        shared_ptr(unique_ptr<_Yp, _Dp>&&,
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3708:9: note: candidate
      constructor template not viable: requires at least 2 arguments, but 1 was provided
        shared_ptr(_Yp* __p, _Dp __d,
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3711:9: note: candidate
      constructor template not viable: requires at least 3 arguments, but 1 was provided
        shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3713:26: note: candidate
      constructor template not viable: requires 2 arguments, but 1 was provided
    template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
                         ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3714: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);
                                       ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3715: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;
                                                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3701:23: note: candidate
      constructor not viable: requires 0 arguments, but 1 was provided
    _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;

KarstenK

Вам нужно найти время и изучить основы C++. Это сэкономит вам много головной боли и потраченного времени.

Member 14068785

Да, именно это я и делаю здесь.
Спасибо!

1 Ответов

Рейтинг:
1

CPallini

Да, это выглядит проблемой компилятора (по крайней мере, g++-7 компилирует его без жалоб).
Почему бы тебе не ... std::array, вместо

auto myArray = std::make_shared< std::array<double,10>>();
?