Monday, May 22, 2023

Digest for comp.lang.c++@googlegroups.com - 9 updates in 1 topic

Muttley@dastardlyhq.com: May 22 08:24AM

On Sat, 20 May 2023 23:53:18 -0400
>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.
Muttley@dastardlyhq.com: May 22 08:31AM

On Sat, 20 May 2023 14:39:39 -0700
>> 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.
Bonita Montero <Bonita.Montero@gmail.com>: May 22 10:47AM +0200


> 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.
Muttley@dastardlyhq.com: May 22 09:22AM

On Mon, 22 May 2023 10:47:17 +0200
>> 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?
"Fred. Zwarts" <F.Zwarts@HetNet.nl>: May 22 11:51AM +0200


> 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.
Muttley@dastardlyhq.com: May 22 10:13AM

On Mon, 22 May 2023 11:51:48 +0200
>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;
}
Bonita Montero <Bonita.Montero@gmail.com>: May 22 05:48PM +0200


> 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.
Muttley@dastardlyhq.com: May 22 04:06PM

On Mon, 22 May 2023 17:48:34 +0200
>> 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.
Bonita Montero <Bonita.Montero@gmail.com>: May 22 06:10PM +0200


> 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.
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: