Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear experts,
I have a simple code which uses shared_ptr with arrays.
The line of the code that gives problem is:
std::shared_ptr<double[]> myArray(new double[10]);

I'm using a Mac OS 10.14.1 and Xcode 10.1 from command line:
c++ -std=c++17 -o sharedPtr sharedPtr.cpp

Apparently I think it should work.
Do you see any issue of compatibility with Mac Xcode C++ compiler?
Many thanks.
- Mauro.

What I have tried:

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;
Posted
Updated 30-Nov-18 0:33am
Comments
KarstenK 30-Nov-18 6:59am    
You neeed to take the time and learn the basics of C++. It will save you a lot of headaches and waisted time.
Member 14068785 30-Nov-18 7:01am    
Yes, that's precisely what I'm doing here.
Thanks!

Yes, it looks a compiler problem (at least g++-7 compiles it without complaining).
Why don't you an std::array, instead
C++
auto myArray = std::make_shared< std::array<double,10>>();
?
 
Share this answer
 
Our code team encountered the same issue with a shared_ptr to an array with Mac's version of the clang compiler. Your syntax is correct and will work with the gcc (not Mac's alias to their clang compiler), llvm, etc. -- we verified the correctness of the coding syntax on Windows and Linux machines using a suite of compilers. I believe the problem is that the Mac version of the clang compiler didn't include the T[] type with the shared_ptr. I was able reproduce the same error message by omitting the T[] in my own implementation of shared_ptr. Our solution is to implement and deploy our own shared_ptr to ensure that our software runs on all systems. Thanks for posting this code error, it was very helpful to know that we were not alone when our code would not run on a Mac with their clang compiler.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900