public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: std^shared_ptr compile error for 2D array type (G++7.2)
       [not found] ` <CAH6eHdTSAaDM0m9eOWnO84q9wxRWqh_84=0ZMPe6Lqvgj_No+g@mail.gmail.com>
@ 2017-10-06 13:27   ` goldenhawking
  0 siblings, 0 replies; 5+ messages in thread
From: goldenhawking @ 2017-10-06 13:27 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: libstdc++

OK! This is a convenient way to learn new standard changesets. By the 
way, our engineers usually take several different ways to maintain 
dynamic m-D array,  but none is perfect.

(1) C-style multi-dimension array pointer: "int (*p)[16][32];"

      Advantage:  continuous memory address for low-level conversion and 
operation.

      Weakness:   only 1 dimension can be dynamically assigned. The size 
'16' and '32' must be constant.

(2) STL container vector: vector< vector< vector<int> > > p;

      Advantage: fully dynamic size assignment for each dimension.

      Weakness:  discontinuous memory address.

(3) Sparse matrix using std::unordered_map: unordered_map<int, 
unordered_map<int, int> > p

      Advantage: memory-saving for sparse matrix .

      Weakness:  discontinuous memory address, low performance.

(4) STL array: array< array< int, MAX1> , MAX2> Darray = {0};

      Advantage: Seems to be continuous memory address?  Mordern STL 
features enabled.

      Weakness:  Dimension size is static. We must predict the max size 
for each dimension.

We choose carefully, and consider that C-style multi-dimension array 
pointer with shared_ptr is the best way for dynamic m-D array.  If 
continuous memory is not required, STL container vector should be the best.

2017-10-06 19:52, Jonathan Wakely:
> You're welcome.
>
> Instead of reading the code to understand what changed it might be
> simpler to see the changes made by the proposal to change the C++
> standard: https://wg21.link/p0414
>
>
>
>
>
> On 6 October 2017 at 12:49, goldenhawking@163.com <goldenhawking@163.com> wrote:
>> Oh, amazing ! I read about the new code of template class in <memory>,
>> really complex , indeed a great job. There seems to be a lot of new
>> knowledge for me to learn, tks!
>>
>> ---Original---
>> From: "Jonathan Wakely"<jwakely.gcc@gmail.com>
>> Date: 2017/10/6 19:25:02
>> To: "goldenhawking"<goldenhawking@163.com>;
>> Cc: "libstdc++"<libstdc++@gcc.gnu.org>;
>> Subject: Re: std::shared_ptr compile error for 2D array type (G++7.2)
>>
>> On 6 October 2017 at 12:02, Jonathan Wakely wrote: > On 6 October 2017 at
>> 11:48, Jonathan Wakely wrote: >> 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>>
>> [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 because that's actually a >> shared_ptr
>> that owns a 2D array. > > Oops, sorry for the type. I meant shared_ptr not
>> "int[]24]" > which isn't a valid type :-) Also you don't need to use the
>> custom deleter, because shared_ptr will do the right thing, and use delete[]
>> to release the memory. So simply: std::shared_ptr< int[][24] > bar(new
>> int[N][24]);


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: std::shared_ptr compile error for 2D array type (G++7.2)
  2017-10-06 11:02   ` Jonathan Wakely
@ 2017-10-06 11:25     ` Jonathan Wakely
  0 siblings, 0 replies; 5+ messages in thread
From: Jonathan Wakely @ 2017-10-06 11:25 UTC (permalink / raw)
  To: goldenhawking; +Cc: libstdc++

On 6 October 2017 at 12:02, Jonathan Wakely wrote:
> On 6 October 2017 at 11:48, Jonathan Wakely wrote:
>> 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.
>
> Oops, sorry for the type. I meant shared_ptr<int[][24]> not "int[]24]"
> which isn't a valid type :-)

Also you don't need to use the custom deleter, because
shared_ptr<int[][24]> will do the right thing, and use delete[] to
release the memory. So simply:

std::shared_ptr< int[][24] > bar(new int[N][24]);

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: std::shared_ptr compile error for 2D array type (G++7.2)
  2017-10-06 10:48 ` Jonathan Wakely
@ 2017-10-06 11:02   ` Jonathan Wakely
  2017-10-06 11:25     ` Jonathan Wakely
  0 siblings, 1 reply; 5+ messages in thread
From: Jonathan Wakely @ 2017-10-06 11:02 UTC (permalink / raw)
  To: goldenhawking; +Cc: libstdc++

On 6 October 2017 at 11:48, Jonathan Wakely wrote:
> 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.

Oops, sorry for the type. I meant shared_ptr<int[][24]> not "int[]24]"
which isn't a valid type :-)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: std::shared_ptr compile error for 2D array type (G++7.2)
  2017-10-06  8:47 std::shared_ptr " goldenhawking
@ 2017-10-06 10:48 ` Jonathan Wakely
  2017-10-06 11:02   ` Jonathan Wakely
  0 siblings, 1 reply; 5+ messages in thread
From: Jonathan Wakely @ 2017-10-06 10:48 UTC (permalink / raw)
  To: goldenhawking; +Cc: libstdc++

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.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* std::shared_ptr compile error for 2D array type (G++7.2)
@ 2017-10-06  8:47 goldenhawking
  2017-10-06 10:48 ` Jonathan Wakely
  0 siblings, 1 reply; 5+ messages in thread
From: goldenhawking @ 2017-10-06  8:47 UTC (permalink / raw)
  To: libstdc++

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.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-10-06 13:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <59d76db6.4cb4630a.96427.f76cSMTPIN_ADDED_BROKEN@mx.google.com>
     [not found] ` <CAH6eHdTSAaDM0m9eOWnO84q9wxRWqh_84=0ZMPe6Lqvgj_No+g@mail.gmail.com>
2017-10-06 13:27   ` std^shared_ptr compile error for 2D array type (G++7.2) goldenhawking
2017-10-06  8:47 std::shared_ptr " goldenhawking
2017-10-06 10:48 ` Jonathan Wakely
2017-10-06 11:02   ` Jonathan Wakely
2017-10-06 11:25     ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).