Saturday, May 20, 2023

Digest for comp.lang.c++@googlegroups.com - 5 updates in 2 topics

Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo>: May 19 11:19PM -0400

> 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.
 
> return 0;
> }
 
> I've tried other combinations too, nothing works.
 
HTH
-Pavel
Muttley@dastardlyhq.com: May 20 08:58AM

On Fri, 19 May 2023 23:19:30 -0400
 
>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.
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: May 20 02:39PM -0700

On 5/20/2023 2:38 PM, Alf P. Steinbach wrote:
 
> `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.
 
"Alf P. Steinbach" <alf.p.steinbach@gmail.com>: May 20 11:38PM +0200

On 2023-05-20 5:19 AM, Pavel wrote:
> 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
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: May 20 01:21PM -0700

On 5/12/2022 12:16 PM, Chris M. Thomasson wrote:
> Using my experimental vector field to generate a fractal formation. Here
> is generation two:
 
> https://fractalforums.org/gallery/1612-120522191048.png
 
Gate to another world. An adventure in equipotentials... ;^)
 
https://i.ibb.co/w79XDxJ/Gates-to-Another-World.png
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: