Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

If Machiavelli were a programmer, he'd have worked for AT&T.


devel / comp.lang.c++ / shared_ptr array range loop doesn't work

SubjectAuthor
* shared_ptr array range loop doesn't workMuttley
+* Re: shared_ptr array range loop doesn't workChris M. Thomasson
|`* Re: shared_ptr array range loop doesn't workMuttley
| `- Re: shared_ptr array range loop doesn't workChris M. Thomasson
+* Re: shared_ptr array range loop doesn't workBonita Montero
|`- Re: shared_ptr array range loop doesn't workMuttley
+* Re: shared_ptr array range loop doesn't workAlf P. Steinbach
|`* Re: shared_ptr array range loop doesn't workMuttley
| `* Re: shared_ptr array range loop doesn't workBonita Montero
|  `- Re: shared_ptr array range loop doesn't workMuttley
+* Re: shared_ptr array range loop doesn't workPavel
|+- Re: shared_ptr array range loop doesn't workMuttley
|`* Re: shared_ptr array range loop doesn't workAlf P. Steinbach
| +* Re: shared_ptr array range loop doesn't workChris M. Thomasson
| |`- Re: shared_ptr array range loop doesn't workMuttley
| `* Re: shared_ptr array range loop doesn't workPavel
|  +* Re: shared_ptr array range loop doesn't workMuttley
|  |+* Re: shared_ptr array range loop doesn't workBonita Montero
|  ||`* Re: shared_ptr array range loop doesn't workMuttley
|  || +* Re: shared_ptr array range loop doesn't workFred. Zwarts
|  || |`- Re: shared_ptr array range loop doesn't workMuttley
|  || `* Re: shared_ptr array range loop doesn't workBonita Montero
|  ||  `* Re: shared_ptr array range loop doesn't workMuttley
|  ||   `* Re: shared_ptr array range loop doesn't workBonita Montero
|  ||    `* Re: shared_ptr array range loop doesn't workMuttley
|  ||     +* Re: shared_ptr array range loop doesn't workBonita Montero
|  ||     |`* Re: shared_ptr array range loop doesn't workMuttley
|  ||     | `- Re: shared_ptr array range loop doesn't workBonita Montero
|  ||     `- Re: shared_ptr array range loop doesn't workPavel
|  |`* Re: shared_ptr array range loop doesn't workPavel
|  | `* Re: shared_ptr array range loop doesn't workÖö Tiib
|  |  `- Re: shared_ptr array range loop doesn't workPavel
|  `* Re: shared_ptr array range loop doesn't workAlf P. Steinbach
|   `* Re: shared_ptr array range loop doesn't workPavel
|    `- Re: shared_ptr array range loop doesn't workChris M. Thomasson
`* Re: shared_ptr array range loop doesn't workjames...@alumni.caltech.edu
 `* Re: shared_ptr array range loop doesn't workMuttley
  +- Re: shared_ptr array range loop doesn't workBonita Montero
  `* Re: shared_ptr array range loop doesn't workjames...@alumni.caltech.edu
   `* Re: shared_ptr array range loop doesn't workMuttley
    `* Re: shared_ptr array range loop doesn't workjames...@alumni.caltech.edu
     `* Re: shared_ptr array range loop doesn't workMuttley
      `* Re: shared_ptr array range loop doesn't workBonita Montero
       `* Re: shared_ptr array range loop doesn't workÖö Tiib
        `* Re: shared_ptr array range loop doesn't workMuttley
         `* Re: shared_ptr array range loop doesn't workBonita Montero
          `- Re: shared_ptr array range loop doesn't workMuttley

Pages:12
shared_ptr array range loop doesn't work

<u44q2h$7ps9$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=161&group=comp.lang.c%2B%2B#161

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.lang.c++
Subject: shared_ptr array range loop doesn't work
Date: Thu, 18 May 2023 09:10:41 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <u44q2h$7ps9$1@dont-email.me>
Injection-Date: Thu, 18 May 2023 09:10:41 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2833c6144ab77e9f37823696bdd021e9";
logging-data="255881"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/q5zK9Pg4ObVdmBeHPMtIN"
Cancel-Lock: sha1:bxFsoAg2tO1HUgIJpHki+4ufHgE=
 by: Muttley@dastardlyhq.com - Thu, 18 May 2023 09:10 UTC

Is there a way to get range loops to work with arrays stored in
shared pointers?

eg:

#include <iostream>
#include <memory>

using namespace std;

struct mystruct
{ mystruct() { cout << "Con " << this << endl; };
~mystruct() { cout << "Dest " << this << endl; }
void func() { cout << "Func " << this << endl; }
};

int main()
{ shared_ptr<mystruct[3]> sp(new mystruct[3]);
sp[0].func();
sp[1].func();
sp[2].func();
for(auto &ms: sp.get()) ms.func(); <--- Doesn't compile
return 0;
}

I've tried other combinations too, nothing works.

Re: shared_ptr array range loop doesn't work

<u44qt3$7rir$3@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=164&group=comp.lang.c%2B%2B#164

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Thu, 18 May 2023 02:24:51 -0700
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <u44qt3$7rir$3@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 18 May 2023 09:24:51 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="19100c6aaa1f5c0d7af75a27f3dc718e";
logging-data="257627"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/wMARLfP5/i8BZ0vS+7rmJDkj/RCStcL0="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:xrH4Oe48OGOSpT2ixCESsTbNW7M=
In-Reply-To: <u44q2h$7ps9$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Thu, 18 May 2023 09:24 UTC

On 5/18/2023 2:10 AM, Muttley@dastardlyhq.com wrote:
> Is there a way to get range loops to work with arrays stored in
> shared pointers?
>
> eg:
>
> #include <iostream>
> #include <memory>
>
> using namespace std;
>
> struct mystruct
> {
> mystruct() { cout << "Con " << this << endl; };
> ~mystruct() { cout << "Dest " << this << endl; }
> void func() { cout << "Func " << this << endl; }
> };
>
>
> int main()
> {
> shared_ptr<mystruct[3]> sp(new mystruct[3]);

Seems a bit odd:

std::vector<std::shared_ptr<mystruct>> vec(3)?

> sp[0].func();
> sp[1].func();
> sp[2].func();
> for(auto &ms: sp.get()) ms.func(); <--- Doesn't compile
> return 0;
> }
>
> I've tried other combinations too, nothing works.
>

Re: shared_ptr array range loop doesn't work

<u455at$90rp$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=165&group=comp.lang.c%2B%2B#165

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Thu, 18 May 2023 14:22:57 +0200
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <u455at$90rp$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 18 May 2023 12:22:53 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="74b8692b95be327c4127402b7fe59aa1";
logging-data="295801"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18SrvUtRQjv+pXoMIzKxl+exBmZxDYe4KA="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:6RXGO2giNwNEamKBmgUbSWMqurs=
Content-Language: de-DE
In-Reply-To: <u44q2h$7ps9$1@dont-email.me>
 by: Bonita Montero - Thu, 18 May 2023 12:22 UTC

Use this:

#include <iostream>
#include <memory>
#include <array>

using namespace std;

struct mystruct
{ mystruct() { cout << "Con " << this << endl; };
~mystruct() { cout << "Dest " << this << endl; }
void func() { cout << "Func " << this << endl; }
};

int main()
{ using arr3_t = array<mystruct, 3>;
shared_ptr<arr3_t> sp( make_shared<arr3_t>() );
(*sp)[0].func();
(*sp)[1].func();
(*sp)[2].func();
for( auto &ms : *sp.get() )
ms.func();
return 0;
}

Re: shared_ptr array range loop doesn't work

<u45es6$a5qt$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=166&group=comp.lang.c%2B%2B#166

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Thu, 18 May 2023 15:05:43 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <u45es6$a5qt$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me> <u44qt3$7rir$3@dont-email.me>
Injection-Date: Thu, 18 May 2023 15:05:43 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2833c6144ab77e9f37823696bdd021e9";
logging-data="333661"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX185ZA9qTta784rMsLxOUT1z"
Cancel-Lock: sha1:UcPwqgiSjZZRghj1q4tk8Uhsh14=
 by: Muttley@dastardlyhq.com - Thu, 18 May 2023 15:05 UTC

On Thu, 18 May 2023 02:24:51 -0700
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> wrote:
>On 5/18/2023 2:10 AM, Muttley@dastardlyhq.com wrote:
>> Is there a way to get range loops to work with arrays stored in
>> shared pointers?
>>
>> eg:
>>
>> #include <iostream>
>> #include <memory>
>>
>> using namespace std;
>>
>> struct mystruct
>> {
>> mystruct() { cout << "Con " << this << endl; };
>> ~mystruct() { cout << "Dest " << this << endl; }
>> void func() { cout << "Func " << this << endl; }
>> };
>>
>>
>> int main()
>> {
>> shared_ptr<mystruct[3]> sp(new mystruct[3]);
>
>Seems a bit odd:
>

Its new with C++ 2017 apparently. I didn't know about this functionality either
until I watched this youtube video:

https://www.youtube.com/watch?v=Tp5-f9YAzNk

Re: shared_ptr array range loop doesn't work

<u45f1g$a6ia$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=167&group=comp.lang.c%2B%2B#167

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Thu, 18 May 2023 15:08:32 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <u45f1g$a6ia$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me> <u455at$90rp$1@dont-email.me>
Injection-Date: Thu, 18 May 2023 15:08:32 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2833c6144ab77e9f37823696bdd021e9";
logging-data="334410"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/+0WsdDcJdNygl44qNT6Jq"
Cancel-Lock: sha1:84uA0SkrJj+ffJ0cuw5XZHqaHTM=
 by: Muttley@dastardlyhq.com - Thu, 18 May 2023 15:08 UTC

On Thu, 18 May 2023 14:22:57 +0200
Bonita Montero <Bonita.Montero@gmail.com> wrote:
>Use this:

I wasn't looking for an alternative solution, there are many. I was just
wondering if its possible to iterate the C array stored in a shared pointer.

Re: shared_ptr array range loop doesn't work

<u45gh3$acsp$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=168&group=comp.lang.c%2B%2B#168

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: alf.p.steinbach@gmail.com (Alf P. Steinbach)
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Thu, 18 May 2023 17:33:53 +0200
Organization: A noiseless patient Spider
Lines: 74
Message-ID: <u45gh3$acsp$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 18 May 2023 15:33:55 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6c0dea3abcde3e6fd944c23479079190";
logging-data="340889"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX183Jf0o7pGhuitDenVTqUKz"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:5A/1zCd01HcNS6+6HAJkU8Gju64=
Content-Language: en-US
In-Reply-To: <u44q2h$7ps9$1@dont-email.me>
 by: Alf P. Steinbach - Thu, 18 May 2023 15:33 UTC

On 2023-05-18 11:10 AM, Muttley@dastardlyhq.com wrote:
> #include <iostream>
> #include <memory>
>
> using namespace std;
>
> struct mystruct
> {
> mystruct() { cout << "Con " << this << endl; };
> ~mystruct() { cout << "Dest " << this << endl; }
> void func() { cout << "Func " << this << endl; }
> };
>
>
> int main()
> {
> shared_ptr<mystruct[3]> sp(new mystruct[3]);
> sp[0].func();
> sp[1].func();
> sp[2].func();
> for(auto &ms: sp.get()) ms.func(); <--- Doesn't compile
> return 0;
> }

Like this:

#include <iostream>
#include <memory>
using namespace std;

//--------------------------------------- Support machinery:
template< class It >
class Its_range_
{ It m_start;
It m_beyond;

public:
Its_range_( const It start, const It beyond ):
m_start( start ),
m_beyond( beyond )
{}

auto begin() const -> It { return m_start; }
auto end() const -> It { return m_beyond; }
};

template< class Item, int n >
auto its_const_range( const shared_ptr<Item[n]> sp )
-> Its_range_<const Item*>
{ return {sp.get(), sp.get() + n}; }

//--------------------------------------- Example:

struct My_struct
{ My_struct() { cout << "Con " << this << endl; };
~My_struct() { cout << "Dest " << this << endl; }
void func() const { cout << "Func " << this << endl; }
};

auto main() -> int
{ shared_ptr<My_struct[3]> sp( new My_struct[3] );
for( auto& ms: its_const_range( sp ) ) {
ms.func();
}
}

- Alf

Re: shared_ptr array range loop doesn't work

<u45hg2$ah2q$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=169&group=comp.lang.c%2B%2B#169

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Thu, 18 May 2023 15:50:26 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <u45hg2$ah2q$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me> <u45gh3$acsp$1@dont-email.me>
Injection-Date: Thu, 18 May 2023 15:50:26 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2833c6144ab77e9f37823696bdd021e9";
logging-data="345178"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/hTUSyHtlk29TwFjcSRs7j"
Cancel-Lock: sha1:IezN5Wsu7h/WQSRB02Hg/9KZZ60=
 by: Muttley@dastardlyhq.com - Thu, 18 May 2023 15:50 UTC

On Thu, 18 May 2023 17:33:53 +0200
"Alf P. Steinbach" <alf.p.steinbach@gmail.com> wrote:
> for( auto& ms: its_const_range( sp ) ) {
> ms.func();
> }

Well yes, it works, but I was rather hoping that there was a built in way
to do it! Seems like this functionality is rather half baked - you can store
an array in shared_ptr but you can't iterate it directly in a range loop.
Might as well just use a vector.

Re: shared_ptr array range loop doesn't work

<u45hth$ahdh$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=170&group=comp.lang.c%2B%2B#170

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Thu, 18 May 2023 17:57:40 +0200
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <u45hth$ahdh$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me> <u45gh3$acsp$1@dont-email.me>
<u45hg2$ah2q$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 18 May 2023 15:57:37 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="74b8692b95be327c4127402b7fe59aa1";
logging-data="345521"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/gfnn9DlYxIvsdfhb/zal7WreEtLlKuKs="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:KoEJPpzlrLXoHo/qWlK85XBJuzg=
Content-Language: de-DE
In-Reply-To: <u45hg2$ah2q$1@dont-email.me>
 by: Bonita Montero - Thu, 18 May 2023 15:57 UTC

Am 18.05.2023 um 17:50 schrieb Muttley@dastardlyhq.com:

> Well yes, it works, but I was rather hoping that there was a built in way
> to do it! Seems like this functionality is rather half baked - you can store
> an array in shared_ptr but you can't iterate it directly in a range loop.
> Might as well just use a vector.

I don't understand why you won't use a std:array<>. I prefer that over
a C-style array because I get bounds-checking for free while debugging.

Re: shared_ptr array range loop doesn't work

<u45i34$ajjq$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=171&group=comp.lang.c%2B%2B#171

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Thu, 18 May 2023 16:00:36 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <u45i34$ajjq$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me> <u45gh3$acsp$1@dont-email.me>
<u45hg2$ah2q$1@dont-email.me>
<u45hth$ahdh$1@dont-email.me>
Injection-Date: Thu, 18 May 2023 16:00:36 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2833c6144ab77e9f37823696bdd021e9";
logging-data="347770"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19J5Cjz5CvyGG+M9Sg4eyb/"
Cancel-Lock: sha1:DaxTQ/1nFLfuWCCyZlROP6SEtc0=
 by: Muttley@dastardlyhq.com - Thu, 18 May 2023 16:00 UTC

On Thu, 18 May 2023 17:57:40 +0200
Bonita Montero <Bonita.Montero@gmail.com> wrote:
>Am 18.05.2023 um 17:50 schrieb Muttley@dastardlyhq.com:
>
>> Well yes, it works, but I was rather hoping that there was a built in way
>> to do it! Seems like this functionality is rather half baked - you can store
>> an array in shared_ptr but you can't iterate it directly in a range loop.
>> Might as well just use a vector.
>
>I don't understand why you won't use a std:array<>. I prefer that over
>a C-style array because I get bounds-checking for free while debugging.

Its some new functionality I learnt about, was just seeing if it was useful.
Seems not. I would never use it myself anyway, plenty of better alternatives.

Re: shared_ptr array range loop doesn't work

<u4619j$c6mp$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=174&group=comp.lang.c%2B%2B#174

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Thu, 18 May 2023 13:20:03 -0700
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <u4619j$c6mp$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me> <u44qt3$7rir$3@dont-email.me>
<u45es6$a5qt$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 18 May 2023 20:20:03 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="19100c6aaa1f5c0d7af75a27f3dc718e";
logging-data="400089"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1//hs4D3DVZpQEQSgPrLlPegsD4vLNf2rk="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:t3CbREdPWa4Aa8M17Sg6FZAPYlA=
Content-Language: en-US
In-Reply-To: <u45es6$a5qt$1@dont-email.me>
 by: Chris M. Thomasson - Thu, 18 May 2023 20:20 UTC

On 5/18/2023 8:05 AM, Muttley@dastardlyhq.com wrote:
> On Thu, 18 May 2023 02:24:51 -0700
> "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> wrote:
>> On 5/18/2023 2:10 AM, Muttley@dastardlyhq.com wrote:
>>> Is there a way to get range loops to work with arrays stored in
>>> shared pointers?
>>>
>>> eg:
>>>
>>> #include <iostream>
>>> #include <memory>
>>>
>>> using namespace std;
>>>
>>> struct mystruct
>>> {
>>> mystruct() { cout << "Con " << this << endl; };
>>> ~mystruct() { cout << "Dest " << this << endl; }
>>> void func() { cout << "Func " << this << endl; }
>>> };
>>>
>>>
>>> int main()
>>> {
>>> shared_ptr<mystruct[3]> sp(new mystruct[3]);
[...]

struct xxx
{ mystruct m_x[3];
};

std::shared_ptr<xxx> Well, what about that fing shit? ;^)

Re: shared_ptr array range loop doesn't work

<bfe02770-89c2-5a3e-860e-1676fadd132d@removeyourself.dontspam.yahoo>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=177&group=comp.lang.c%2B%2B#177

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!1.us.feeder.erje.net!feeder.erje.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx14.iad.POSTED!not-for-mail
Subject: Re: shared_ptr array range loop doesn't work
Newsgroups: comp.lang.c++
References: <u44q2h$7ps9$1@dont-email.me>
From: pauldontspamtolk@removeyourself.dontspam.yahoo (Pavel)
Message-ID: <bfe02770-89c2-5a3e-860e-1676fadd132d@removeyourself.dontspam.yahoo>
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.15
MIME-Version: 1.0
In-Reply-To: <u44q2h$7ps9$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 51
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Sat, 20 May 2023 03:19:39 UTC
Date: Fri, 19 May 2023 23:19:30 -0400
X-Received-Bytes: 2249
 by: Pavel - Sat, 20 May 2023 03:19 UTC

Muttley@dastardlyhq.com wrote:
> Is there a way to get range loops to work with arrays stored in
> shared pointers?
These shared pointers do not store arrays. They store pointers to memory
object allocated with operator new[].

In other words, the type shared_ptr<mystruct[3]>::element_type is same
as shared_ptr<mystruct>::element_type i.e. mystruct.

To get a feeling of why it's natural, note that the type of the
expression "new mystruct[3]" that you use to initalize sp is mystruct*,
not mystruct (*)[3].

Therefore, it is not possible to iterate the way you want: there is no
either constexpr 3 anywhere in the type of shared_ptr<mystruct[3]> or
non-constexpr 3 that could be extracted from the object in memory in any
implementation-independent way. Thus, there is no way for C++ compiler
to generate the code that would compute the end of the range.

>
> eg:
>
> #include <iostream>
> #include <memory>
>
> using namespace std;
>
> struct mystruct
> {
> mystruct() { cout << "Con " << this << endl; };
> ~mystruct() { cout << "Dest " << this << endl; }
> void func() { cout << "Func " << this << endl; }
> };
>
>
> int main()
> {
> shared_ptr<mystruct[3]> sp(new mystruct[3]);
> sp[0].func();
> sp[1].func();
> sp[2].func();
> for(auto &ms: sp.get()) ms.func(); <--- Doesn't compile
> return 0;
> }
>
> I've tried other combinations too, nothing works.
>

HTH
-Pavel

Re: shared_ptr array range loop doesn't work

<u4a23r$11amg$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=178&group=comp.lang.c%2B%2B#178

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Sat, 20 May 2023 08:58:35 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <u4a23r$11amg$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me> <bfe02770-89c2-5a3e-860e-1676fadd132d@removeyourself.dontspam.yahoo>
Injection-Date: Sat, 20 May 2023 08:58:35 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d6fa0bb8445cebda3307f2399ec2adc8";
logging-data="1092304"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Or+Q27CisRV1y7c/WtpEz"
Cancel-Lock: sha1:nB4zmA59kTSfLAcrAA1e3CouPko=
 by: Muttley@dastardlyhq.com - Sat, 20 May 2023 08:58 UTC

On Fri, 19 May 2023 23:19:30 -0400
Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> wrote:
>Muttley@dastardlyhq.com wrote:
>> Is there a way to get range loops to work with arrays stored in
>> shared pointers?
>These shared pointers do not store arrays. They store pointers to memory
>object allocated with operator new[].
>
>In other words, the type shared_ptr<mystruct[3]>::element_type is same
>as shared_ptr<mystruct>::element_type i.e. mystruct.
>
>To get a feeling of why it's natural, note that the type of the
>expression "new mystruct[3]" that you use to initalize sp is mystruct*,
>not mystruct (*)[3].

Fair enough. Does make me wonder why they bothered to introduce this
particular semantic for shared pointers.

Re: shared_ptr array range loop doesn't work

<u4bek2$169ir$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=180&group=comp.lang.c%2B%2B#180

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: alf.p.steinbach@gmail.com (Alf P. Steinbach)
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Sat, 20 May 2023 23:38:09 +0200
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <u4bek2$169ir$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me>
<bfe02770-89c2-5a3e-860e-1676fadd132d@removeyourself.dontspam.yahoo>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 20 May 2023 21:38:10 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b6ccc94e77c8750f16ebe642813bdaaa";
logging-data="1255003"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+BvGWlzickLq9GcPUroWrH"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:nI5Y3q22EBLdCJhrbG51GkRu/iQ=
Content-Language: en-US
In-Reply-To: <bfe02770-89c2-5a3e-860e-1676fadd132d@removeyourself.dontspam.yahoo>
 by: Alf P. Steinbach - Sat, 20 May 2023 21:38 UTC

On 2023-05-20 5:19 AM, Pavel wrote:
> Muttley@dastardlyhq.com wrote:
>> Is there a way to get range loops to work with arrays stored in
>> shared pointers?
> These shared pointers do not store arrays. They store pointers to memory
> object allocated with operator new[].
>
> In other words, the type shared_ptr<mystruct[3]>::element_type is same
> as shared_ptr<mystruct>::element_type i.e. mystruct.
>
> To get a feeling of why it's natural, note that the type of the
> expression "new mystruct[3]" that you use to initalize sp is mystruct*,
> not mystruct (*)[3].
>
> Therefore, it is not possible to iterate the way you want: there is no
> either constexpr 3 anywhere in the type of shared_ptr<mystruct[3]> or
> non-constexpr 3 that could be extracted from the object in memory in any
> implementation-independent way. Thus, there is no way for C++ compiler
> to generate the code that would compute the end of the range.

Oh, there is such a way.

`shared_ptr` currently makes available an indexing operator if the item
type is array: <url:
https://en.cppreference.com/w/cpp/memory/shared_ptr/operator_at>.

It could just as easily make available `begin` and `end` operations for
array item type, and then a range based loop would Just Work. My earlier
answer in this thread showed how to do that oneself. And what I can do
(it was trivial enough that a dino could manage) the committee could do.

- Alf

Re: shared_ptr array range loop doesn't work

<u4bemr$168fv$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=181&group=comp.lang.c%2B%2B#181

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Sat, 20 May 2023 14:39:39 -0700
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <u4bemr$168fv$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me>
<bfe02770-89c2-5a3e-860e-1676fadd132d@removeyourself.dontspam.yahoo>
<u4bek2$169ir$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 20 May 2023 21:39:39 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="1fdaa9981b36f1644944990f845b1003";
logging-data="1253887"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19QBSlpJKOue1+a3b7Ge6IZ3E7LFZRO4BQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:zBkY6Gsva+6uLsNQMVz/CMfbU2k=
Content-Language: en-US
In-Reply-To: <u4bek2$169ir$1@dont-email.me>
 by: Chris M. Thomasson - Sat, 20 May 2023 21:39 UTC

On 5/20/2023 2:38 PM, Alf P. Steinbach wrote:
> On 2023-05-20 5:19 AM, Pavel wrote:
>> Muttley@dastardlyhq.com wrote:
>>> Is there a way to get range loops to work with arrays stored in
>>> shared pointers?
>> These shared pointers do not store arrays. They store pointers to
>> memory object allocated with operator new[].
>>
>> In other words, the type shared_ptr<mystruct[3]>::element_type is same
>> as shared_ptr<mystruct>::element_type i.e. mystruct.
>>
>> To get a feeling of why it's natural, note that the type of the
>> expression "new mystruct[3]" that you use to initalize sp is
>> mystruct*, not mystruct (*)[3].
>>
>> Therefore, it is not possible to iterate the way you want: there is no
>> either constexpr 3 anywhere in the type of shared_ptr<mystruct[3]> or
>> non-constexpr 3 that could be extracted from the object in memory in
>> any implementation-independent way. Thus, there is no way for C++
>> compiler to generate the code that would compute the end of the range.
>
> Oh, there is such a way.
>
> `shared_ptr` currently makes available an indexing operator if the item
> type is array: <url:
> https://en.cppreference.com/w/cpp/memory/shared_ptr/operator_at>.

Thanks! I did not know that.

>
> It could just as easily make available `begin` and `end` operations for
> array item type, and then a range based loop would Just Work. My earlier
> answer in this thread showed how to do that oneself. And what I can do
> (it was trivial enough that a dino could manage) the committee could do.

Re: shared_ptr array range loop doesn't work

<WAgaM.571516$mmyc.42481@fx37.iad>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=182&group=comp.lang.c%2B%2B#182

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx37.iad.POSTED!not-for-mail
Subject: Re: shared_ptr array range loop doesn't work
Newsgroups: comp.lang.c++
References: <u44q2h$7ps9$1@dont-email.me>
<bfe02770-89c2-5a3e-860e-1676fadd132d@removeyourself.dontspam.yahoo>
<u4bek2$169ir$1@dont-email.me>
From: pauldontspamtolk@removeyourself.dontspam.yahoo (Pavel)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.15
MIME-Version: 1.0
In-Reply-To: <u4bek2$169ir$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 59
Message-ID: <WAgaM.571516$mmyc.42481@fx37.iad>
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Sun, 21 May 2023 03:53:26 UTC
Date: Sat, 20 May 2023 23:53:18 -0400
X-Received-Bytes: 3121
 by: Pavel - Sun, 21 May 2023 03:53 UTC

Alf P. Steinbach wrote:
> On 2023-05-20 5:19 AM, Pavel wrote:
>> Muttley@dastardlyhq.com wrote:
>>> Is there a way to get range loops to work with arrays stored in
>>> shared pointers?
>> These shared pointers do not store arrays. They store pointers to
>> memory object allocated with operator new[].
>>
>> In other words, the type shared_ptr<mystruct[3]>::element_type is same
>> as shared_ptr<mystruct>::element_type i.e. mystruct.
>>
>> To get a feeling of why it's natural, note that the type of the
>> expression "new mystruct[3]" that you use to initalize sp is
>> mystruct*, not mystruct (*)[3].
>>
>> Therefore, it is not possible to iterate the way you want: there is no
>> either constexpr 3 anywhere in the type of shared_ptr<mystruct[3]> or
>> non-constexpr 3 that could be extracted from the object in memory in
>> any implementation-independent way. Thus, there is no way for C++
>> compiler to generate the code that would compute the end of the range.
>
> Oh, there is such a way.
Not without changing the Standard. My understanding is that OP tried to
make his code iterate with the shared pointers *as they are currently
defined* in the Standard.

>
> `shared_ptr` currently makes available an indexing operator if the item
> type is array: <url:
> https://en.cppreference.com/w/cpp/memory/shared_ptr/operator_at>.
>
> It could just as easily make available `begin` and `end` operations for
> array item type, and then a range based loop would Just Work. My earlier
> answer in this thread showed how to do that oneself. And what I can do
> (it was trivial enough that a dino could manage) the committee could do.
They could, for the template parameter of an array type U[N]. It is
unclear what is supposed to be done for the parameter of an array type U[].

But, with the current Standard, the N is lost. I am ok with this though
as adding this "feature" would open the whole new can of warms for
unclear benefit.

E.g. assume that, in line with your example, N becomes part of
shared_ptr instatiation *type* and consider this definition:

shared_ptr<mystruct[3]> sp(new mystruct[2]);

or, even better:

shared_ptr<mystruct[3]> sp(new mystruct[getNOfMyStructs()]);

What can possibly go wrong? (TM)

>
>
> - Alf
>

-Pavel

Re: shared_ptr array range loop doesn't work

<u4f8s7$249jm$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=183&group=comp.lang.c%2B%2B#183

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Mon, 22 May 2023 08:24:39 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <u4f8s7$249jm$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me>
<bfe02770-89c2-5a3e-860e-1676fadd132d@removeyourself.dontspam.yahoo>
<u4bek2$169ir$1@dont-email.me>
<WAgaM.571516$mmyc.42481@fx37.iad>
Injection-Date: Mon, 22 May 2023 08:24:39 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="47e84eaaeb78b5216c199b57f07b27f4";
logging-data="2238070"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/epuDgUoZQBEUD7YjtU+Jh"
Cancel-Lock: sha1:Hh8hPGbSoJa+mErqsqRY+juUkSs=
 by: Muttley@dastardlyhq.com - Mon, 22 May 2023 08:24 UTC

On Sat, 20 May 2023 23:53:18 -0400
Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> wrote:
>Alf P. Steinbach wrote:
>> It could just as easily make available `begin` and `end` operations for
>> array item type, and then a range based loop would Just Work. My earlier
>> answer in this thread showed how to do that oneself. And what I can do
>> (it was trivial enough that a dino could manage) the committee could do.
>They could, for the template parameter of an array type U[N]. It is
>unclear what is supposed to be done for the parameter of an array type U[].
>
>But, with the current Standard, the N is lost. I am ok with this though

N might be lost to the user but the compiler/runtime is quite well aware of
what it is otherwise delete[] couldn't work which makes me wonder why its
not made available to range based loops.

Re: shared_ptr array range loop doesn't work

<u4f98t$24b4j$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=184&group=comp.lang.c%2B%2B#184

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Mon, 22 May 2023 08:31:25 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <u4f98t$24b4j$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me>
<bfe02770-89c2-5a3e-860e-1676fadd132d@removeyourself.dontspam.yahoo>
<u4bek2$169ir$1@dont-email.me>
<u4bemr$168fv$1@dont-email.me>
Injection-Date: Mon, 22 May 2023 08:31:25 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="47e84eaaeb78b5216c199b57f07b27f4";
logging-data="2239635"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+w1r7VIffSq61g8l85ued9"
Cancel-Lock: sha1:GAMMv9hKaMtSZpRBdhKJYgN9yUA=
 by: Muttley@dastardlyhq.com - Mon, 22 May 2023 08:31 UTC

On Sat, 20 May 2023 14:39:39 -0700
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> wrote:
>On 5/20/2023 2:38 PM, Alf P. Steinbach wrote:
>> Oh, there is such a way.
>>
>> `shared_ptr` currently makes available an indexing operator if the item
>> type is array: <url:
>> https://en.cppreference.com/w/cpp/memory/shared_ptr/operator_at>.
>
>Thanks! I did not know that.

Neither did I until I watched the youtube video I linked to a few posts
back. Thats the problem with modern C++, the language is far too big for any
one person to know (probably even Bjarne). Whenever I come across something
new I write a little test program with come keywords in the comments so I
can just grep for them when I need similar functionality in the future because
I know I'm never going to remember all of them if I don't use them much.

Re: shared_ptr array range loop doesn't work

<u4fa6g$24edb$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=185&group=comp.lang.c%2B%2B#185

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Mon, 22 May 2023 10:47:17 +0200
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <u4fa6g$24edb$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me>
<bfe02770-89c2-5a3e-860e-1676fadd132d@removeyourself.dontspam.yahoo>
<u4bek2$169ir$1@dont-email.me> <WAgaM.571516$mmyc.42481@fx37.iad>
<u4f8s7$249jm$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 22 May 2023 08:47:12 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d8527a65ea490069f139d3d1d917d38c";
logging-data="2242987"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/zqq1SRh/MyLr0OWqdyH4HxKZtoC/VpDg="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:YRhlt+Z8Tks+hwomOeXRJkKdOks=
In-Reply-To: <u4f8s7$249jm$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Mon, 22 May 2023 08:47 UTC

Am 22.05.2023 um 10:24 schrieb Muttley@dastardlyhq.com:

> N might be lost to the user but the compiler/runtime is quite well aware of
> what it is otherwise delete[] couldn't work which makes me wonder why its
> not made available to range based loops.

There are specializations for array'd shared_ptr-s / unique_ptr-s.

Re: shared_ptr array range loop doesn't work

<u4fc9e$24mpi$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=186&group=comp.lang.c%2B%2B#186

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Mon, 22 May 2023 09:22:54 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <u4fc9e$24mpi$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me>
<bfe02770-89c2-5a3e-860e-1676fadd132d@removeyourself.dontspam.yahoo>
<u4bek2$169ir$1@dont-email.me> <WAgaM.571516$mmyc.42481@fx37.iad>
<u4f8s7$249jm$1@dont-email.me>
<u4fa6g$24edb$1@dont-email.me>
Injection-Date: Mon, 22 May 2023 09:22:54 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="47e84eaaeb78b5216c199b57f07b27f4";
logging-data="2251570"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+VXhtWszYI4xW6u02h8TrU"
Cancel-Lock: sha1:i45kl5kfqoy4hFHuIlzfbZ1xyUY=
 by: Muttley@dastardlyhq.com - Mon, 22 May 2023 09:22 UTC

On Mon, 22 May 2023 10:47:17 +0200
Bonita Montero <Bonita.Montero@gmail.com> wrote:
>Am 22.05.2023 um 10:24 schrieb Muttley@dastardlyhq.com:
>
>> N might be lost to the user but the compiler/runtime is quite well aware of
>> what it is otherwise delete[] couldn't work which makes me wonder why its
>> not made available to range based loops.
>
>There are specializations for array'd shared_ptr-s / unique_ptr-s.
>

You missed the point. The compiler is well aware of how many elements are
in the array pointed to by a C pointer created by new[] so why not extend that
awareness to range based loops without any specialisations being required?

Re: shared_ptr array range loop doesn't work

<u4fdvk$24pol$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=187&group=comp.lang.c%2B%2B#187

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: F.Zwarts@HetNet.nl (Fred. Zwarts)
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Mon, 22 May 2023 11:51:48 +0200
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <u4fdvk$24pol$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me>
<bfe02770-89c2-5a3e-860e-1676fadd132d@removeyourself.dontspam.yahoo>
<u4bek2$169ir$1@dont-email.me> <WAgaM.571516$mmyc.42481@fx37.iad>
<u4f8s7$249jm$1@dont-email.me> <u4fa6g$24edb$1@dont-email.me>
<u4fc9e$24mpi$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 22 May 2023 09:51:48 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="0da99fd589320ad9c1dcb80c5e49f298";
logging-data="2254613"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+WHZ1N9vaPoGlhKY15l5Hi"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:eOockb5dIYyxEicE3yrmYlYH5yg=
Content-Language: en-GB
In-Reply-To: <u4fc9e$24mpi$1@dont-email.me>
 by: Fred. Zwarts - Mon, 22 May 2023 09:51 UTC

Op 22.mei.2023 om 11:22 schreef Muttley@dastardlyhq.com:
> On Mon, 22 May 2023 10:47:17 +0200
> Bonita Montero <Bonita.Montero@gmail.com> wrote:
>> Am 22.05.2023 um 10:24 schrieb Muttley@dastardlyhq.com:
>>
>>> N might be lost to the user but the compiler/runtime is quite well aware of
>>> what it is otherwise delete[] couldn't work which makes me wonder why its
>>> not made available to range based loops.
>>
>> There are specializations for array'd shared_ptr-s / unique_ptr-s.
>>
>
> You missed the point. The compiler is well aware of how many elements are
> in the array pointed to by a C pointer created by new[] so why not extend that
> awareness to range based loops without any specialisations being required?
>

That is difficult to specify in de standard, because it depends very
much on the context. Given a pointer in an arbitrary context how could
the compiler know that it was created by new[]? There is a reason that
there is a delete and a delete[], because the compiler does not always
know from a pointer alone how it was created, so the programmer has to
help the compiler. Only if the creation of the pointer with new[] is
visible by the compiler, then it could know use that information, but
that is not always the case.

Re: shared_ptr array range loop doesn't work

<u4ff8c$251u8$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=188&group=comp.lang.c%2B%2B#188

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Mon, 22 May 2023 10:13:32 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <u4ff8c$251u8$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me>
<bfe02770-89c2-5a3e-860e-1676fadd132d@removeyourself.dontspam.yahoo>
<u4bek2$169ir$1@dont-email.me> <WAgaM.571516$mmyc.42481@fx37.iad>
<u4f8s7$249jm$1@dont-email.me> <u4fa6g$24edb$1@dont-email.me>
<u4fc9e$24mpi$1@dont-email.me>
<u4fdvk$24pol$1@dont-email.me>
Injection-Date: Mon, 22 May 2023 10:13:32 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="47e84eaaeb78b5216c199b57f07b27f4";
logging-data="2262984"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19zrnIN3/GdKUNnViGORvrp"
Cancel-Lock: sha1:LRQtf/AvPAh0I5DSHvRYXUwdoAU=
 by: Muttley@dastardlyhq.com - Mon, 22 May 2023 10:13 UTC

On Mon, 22 May 2023 11:51:48 +0200
"Fred. Zwarts" <F.Zwarts@HetNet.nl> wrote:
>Op 22.mei.2023 om 11:22 schreef Muttley@dastardlyhq.com:
>> On Mon, 22 May 2023 10:47:17 +0200
>> Bonita Montero <Bonita.Montero@gmail.com> wrote:
>>> Am 22.05.2023 um 10:24 schrieb Muttley@dastardlyhq.com:
>>>
>>>> N might be lost to the user but the compiler/runtime is quite well aware of
>
>>>> what it is otherwise delete[] couldn't work which makes me wonder why its
>>>> not made available to range based loops.
>>>
>>> There are specializations for array'd shared_ptr-s / unique_ptr-s.
>>>
>>
>> You missed the point. The compiler is well aware of how many elements are
>> in the array pointed to by a C pointer created by new[] so why not extend
>that
>> awareness to range based loops without any specialisations being required?
>>
>
>That is difficult to specify in de standard, because it depends very
>much on the context. Given a pointer in an arbitrary context how could
>the compiler know that it was created by new[]? There is a reason that
>there is a delete and a delete[], because the compiler does not always
>know from a pointer alone how it was created, so the programmer has to
>help the compiler. Only if the creation of the pointer with new[] is
>visible by the compiler, then it could know use that information, but
>that is not always the case.

Ok, so if the *ms pointer below has no attached metadata other than the raw
memory size allocated how does delete[] know to iterate through 3 elements?
Does it literally divide the memory allocation size by individual object size
and use that as the count? There's no reason that approach couldn't be used
in range based loops too.

using namespace std;

struct mystruct
{
mystruct() { cout << "Con " << this << endl; };
~mystruct() { cout << "Dest " << this << endl; }
};

int main()
{ mystruct *ms = new mystruct[3];
delete[] ms;
return 0;
}

Re: shared_ptr array range loop doesn't work

<u4g2sb$27344$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=189&group=comp.lang.c%2B%2B#189

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Mon, 22 May 2023 17:48:34 +0200
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <u4g2sb$27344$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me>
<bfe02770-89c2-5a3e-860e-1676fadd132d@removeyourself.dontspam.yahoo>
<u4bek2$169ir$1@dont-email.me> <WAgaM.571516$mmyc.42481@fx37.iad>
<u4f8s7$249jm$1@dont-email.me> <u4fa6g$24edb$1@dont-email.me>
<u4fc9e$24mpi$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 22 May 2023 15:48:27 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d8527a65ea490069f139d3d1d917d38c";
logging-data="2329732"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19acXdjuD9ExV669ytfuBuMidZZlLy7U0E="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:VIkzII8sn5PCDZhI6sdAG4ww1Sk=
Content-Language: de-DE
In-Reply-To: <u4fc9e$24mpi$1@dont-email.me>
 by: Bonita Montero - Mon, 22 May 2023 15:48 UTC

Am 22.05.2023 um 11:22 schrieb Muttley@dastardlyhq.com:
> On Mon, 22 May 2023 10:47:17 +0200
> Bonita Montero <Bonita.Montero@gmail.com> wrote:
>> Am 22.05.2023 um 10:24 schrieb Muttley@dastardlyhq.com:
>>
>>> N might be lost to the user but the compiler/runtime is quite well aware of
>>> what it is otherwise delete[] couldn't work which makes me wonder why its
>>> not made available to range based loops.
>>
>> There are specializations for array'd shared_ptr-s / unique_ptr-s.
>>
>
> You missed the point. The compiler is well aware of how many elements are
> in the array pointed to by a C pointer created by new[] so why not extend that
> awareness to range based loops without any specialisations being required?

Because the shared_ptr would need a .begin(), .end() and further
things required for a loop. And C-style arrays are outdated in
favor of std::array<> anyway.

Re: shared_ptr array range loop doesn't work

<u4g3ti$276ke$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=190&group=comp.lang.c%2B%2B#190

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Mon, 22 May 2023 16:06:10 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <u4g3ti$276ke$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me>
<bfe02770-89c2-5a3e-860e-1676fadd132d@removeyourself.dontspam.yahoo>
<u4bek2$169ir$1@dont-email.me> <WAgaM.571516$mmyc.42481@fx37.iad>
<u4f8s7$249jm$1@dont-email.me> <u4fa6g$24edb$1@dont-email.me>
<u4fc9e$24mpi$1@dont-email.me>
<u4g2sb$27344$1@dont-email.me>
Injection-Date: Mon, 22 May 2023 16:06:10 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="47e84eaaeb78b5216c199b57f07b27f4";
logging-data="2333326"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Gm3K0h+3uk7nkoFhRa/8b"
Cancel-Lock: sha1:1JMgQonDAfNf1tKELp/5FBnhIJs=
 by: Muttley@dastardlyhq.com - Mon, 22 May 2023 16:06 UTC

On Mon, 22 May 2023 17:48:34 +0200
Bonita Montero <Bonita.Montero@gmail.com> wrote:
>Am 22.05.2023 um 11:22 schrieb Muttley@dastardlyhq.com:
>> On Mon, 22 May 2023 10:47:17 +0200
>> Bonita Montero <Bonita.Montero@gmail.com> wrote:
>>> Am 22.05.2023 um 10:24 schrieb Muttley@dastardlyhq.com:
>>>
>>>> N might be lost to the user but the compiler/runtime is quite well aware of
>
>>>> what it is otherwise delete[] couldn't work which makes me wonder why its
>>>> not made available to range based loops.
>>>
>>> There are specializations for array'd shared_ptr-s / unique_ptr-s.
>>>
>>
>> You missed the point. The compiler is well aware of how many elements are
>> in the array pointed to by a C pointer created by new[] so why not extend
>that
>> awareness to range based loops without any specialisations being required?
>
>Because the shared_ptr would need a .begin(), .end() and further
>things required for a loop. And C-style arrays are outdated in

They could easily be implemented implicitly just like here:

char s[] = "hello";
for(auto c: s) cout << c;

But yet again you missed the point.

Re: shared_ptr array range loop doesn't work

<u4g467$2773m$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=191&group=comp.lang.c%2B%2B#191

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: shared_ptr array range loop doesn't work
Date: Mon, 22 May 2023 18:10:54 +0200
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <u4g467$2773m$1@dont-email.me>
References: <u44q2h$7ps9$1@dont-email.me>
<bfe02770-89c2-5a3e-860e-1676fadd132d@removeyourself.dontspam.yahoo>
<u4bek2$169ir$1@dont-email.me> <WAgaM.571516$mmyc.42481@fx37.iad>
<u4f8s7$249jm$1@dont-email.me> <u4fa6g$24edb$1@dont-email.me>
<u4fc9e$24mpi$1@dont-email.me> <u4g2sb$27344$1@dont-email.me>
<u4g3ti$276ke$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 22 May 2023 16:10:47 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d8527a65ea490069f139d3d1d917d38c";
logging-data="2333814"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19kaz2AeQqnTq8Z0+xQaqNFamsCJEVPiUc="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:9+i5qCYXpQS8i+l1B9vGyqy2cpw=
Content-Language: de-DE
In-Reply-To: <u4g3ti$276ke$1@dont-email.me>
 by: Bonita Montero - Mon, 22 May 2023 16:10 UTC

Am 22.05.2023 um 18:06 schrieb Muttley@dastardlyhq.com:

> They could easily be implemented implicitly just like here:
>
> char s[] = "hello";
> for(auto c: s) cout << c;
>
> But yet again you missed the point.

No, if the shared_ptr should behave like a container you'd need
an iterator-type with * and ++ and a begin and end-method on the
shared_ptr. I think the C++ designers thought this isn't necessary.

Re: shared_ptr array range loop doesn't work

<riWaM.2057692$MVg8.1935284@fx12.iad>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=192&group=comp.lang.c%2B%2B#192

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx12.iad.POSTED!not-for-mail
Subject: Re: shared_ptr array range loop doesn't work
Newsgroups: comp.lang.c++
References: <u44q2h$7ps9$1@dont-email.me>
<bfe02770-89c2-5a3e-860e-1676fadd132d@removeyourself.dontspam.yahoo>
<u4bek2$169ir$1@dont-email.me> <WAgaM.571516$mmyc.42481@fx37.iad>
<u4f8s7$249jm$1@dont-email.me>
From: pauldontspamtolk@removeyourself.dontspam.yahoo (Pavel)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.15
MIME-Version: 1.0
In-Reply-To: <u4f8s7$249jm$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 55
Message-ID: <riWaM.2057692$MVg8.1935284@fx12.iad>
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Tue, 23 May 2023 03:20:55 UTC
Date: Mon, 22 May 2023 23:20:43 -0400
X-Received-Bytes: 3259
 by: Pavel - Tue, 23 May 2023 03:20 UTC

Muttley@dastardlyhq.com wrote:
> On Sat, 20 May 2023 23:53:18 -0400
> Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> wrote:
>> Alf P. Steinbach wrote:
>>> It could just as easily make available `begin` and `end` operations for
>>> array item type, and then a range based loop would Just Work. My earlier
>>> answer in this thread showed how to do that oneself. And what I can do
>>> (it was trivial enough that a dino could manage) the committee could do.
>> They could, for the template parameter of an array type U[N]. It is
>> unclear what is supposed to be done for the parameter of an array type U[].
>>
>> But, with the current Standard, the N is lost. I am ok with this though
>
> N might be lost to the user but the compiler/runtime is quite well aware of
> what it is otherwise delete[] couldn't work which makes me wonder why its
> not made available to range based loops.

Well, there are (up to) 2 different Ns. First N is a bound of template
parameter when it is an array with a known bound. Second N is that
number of elements that compiler knows (in the implementation-dependent
way) when array delete expression destroys the objects.

If begin() and end() on shared_ptr were defined, would these lines
remain legal C++ code?

shared_ptr<mystruct[3]> spt1(new mystruct[2]); // (a)

shared_ptr<mystruct[2]> spt2(new mystrucct[3]); // (b)

shared_ptr<mystruct[2]> spt3(getNFromFile()); // (c)

?

If yes, how many elements would range loop iterate thru?

I think maybe if the Standard only allowed shared_ptr specialization
with an array of unknown bound, the behavior of end() would be
technically easier to define (but still difficult to implement as any
implementation-specific feature).

Still, this would take us beyond smart pointer design goal -- which is
in essence to improve on safety of a raw pointer while preserving as
many of the raw pointer capabilities as possible. If we need an array,
we have std::array; if we have a vector, we have std::vector and
std::valarray.

(I personally think it is a shame that we don't have an exact-size
vector (i.e. a vector sized like valarray, with no capacities) but still
it is not a good excuse to abuse poor little smart pointers. For the
exact-size vector, using new[]- allocated memory and compiler trickery
for N could be justified).

-Pavel

Pages:12
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor