This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: std::shared_ptr compile error for 2D array type (G++7.2)
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: goldenhawking <goldenhawking at 163 dot com>
- Cc: libstdc++ at gcc dot gnu dot org
- Date: Fri, 6 Oct 2017 11:48:06 +0100
- Subject: Re: std::shared_ptr compile error for 2D array type (G++7.2)
- Authentication-results: sourceware.org; auth=none
- References: <8b4734be-720b-6ce3-74b7-d179fb53a800@163.com>
On 6 October 2017 at 09:47, goldenhawking wrote:
> We use std::shared_ptr to hold 2D array like this:
>
> std::shared_ptr< int[24] > bar(new int[N][24], [=](int(*p)[24])->void {
> delete[] p; } );
>
> GNU C++ 4/5/6 is ok . GNU C++ 7.2 gives an error message:
>
> error: no matching function for call to 'std::shared_ptr<int
> [24]>::shared_ptr(int (*)[24])' shared_ptr< int [24] > pt (new int
> [4096][24]);
>
> This may be a problem caused by default construct mechanism of a 2D array.
No, it's because in C++11 and C++14 shared_ptr was not designed to be
used with arrays. In C++17 arrays are fully supported, but in a way
that is incompatible with your code.
It will work if you use shared_ptr<int[]24]> because that's actually a
shared_ptr that owns a 2D array.