Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #117642 > unrolled thread

What's decltype doing here?

Started byBen Bacarisse <ben.usenet@bsb.me.uk>
First post2023-11-07 10:11 +0000
Last post2023-11-08 20:55 -0800
Articles 19 — 6 participants

Back to article view | Back to comp.lang.c++


Contents

  What's decltype doing here? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-11-07 10:11 +0000
    Re: What's decltype doing here? Bo Persson <bo@bo-persson.se> - 2023-11-07 12:12 +0100
      Re: What's decltype doing here? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-11-07 15:16 +0000
        Re: What's decltype doing here? Bonita Montero <Bonita.Montero@gmail.com> - 2023-11-07 16:22 +0100
    Re: What's decltype doing here? Bonita Montero <Bonita.Montero@gmail.com> - 2023-11-07 13:23 +0100
      Re: What's decltype doing here? David Brown <david.brown@hesbynett.no> - 2023-11-07 15:38 +0100
        Re: What's decltype doing here? Bonita Montero <Bonita.Montero@gmail.com> - 2023-11-07 16:38 +0100
          Re: What's decltype doing here? scott@slp53.sl.home (Scott Lurndal) - 2023-11-07 16:04 +0000
            Re: What's decltype doing here? Bonita Montero <Bonita.Montero@gmail.com> - 2023-11-07 17:16 +0100
          Re: What's decltype doing here? David Brown <david.brown@hesbynett.no> - 2023-11-07 17:45 +0100
            Re: What's decltype doing here? Bonita Montero <Bonita.Montero@gmail.com> - 2023-11-07 17:49 +0100
              Re: What's decltype doing here? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-11-07 11:59 -0800
                Re: What's decltype doing here? Bonita Montero <Bonita.Montero@gmail.com> - 2023-11-08 07:28 +0100
                  Re: What's decltype doing here? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-11-08 13:52 -0800
        Re: What's decltype doing here? David Brown <david.brown@hesbynett.no> - 2023-11-08 09:45 +0100
          Re: What's decltype doing here? Bonita Montero <Bonita.Montero@gmail.com> - 2023-11-08 10:01 +0100
          Re: What's decltype doing here? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-11-08 13:54 -0800
            Re: What's decltype doing here? Bonita Montero <Bonita.Montero@gmail.com> - 2023-11-09 05:50 +0100
              Re: What's decltype doing here? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-11-08 20:55 -0800

#117642 — What's decltype doing here?

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2023-11-07 10:11 +0000
SubjectWhat's decltype doing here?
Message-ID<878r7950vy.fsf@bsb.me.uk>
This question got a bit lost in another thread, so I am re-posting it on
its own.  What is decltype doing in the following short program?

  #include <iostream>
  #include <memory>
   
  int main()
  {
     int x = 42;
     std::unique_ptr<int, decltype([](int *p) {
        std::cout << "delete (" << *p << ")\n";
     })> upi(&x);
  }

I expected decltype to supply just a type in the template and for a
second argument to be required when constructing upi, but no.  The
program compiles (gcc and clang) and prints "delete (42)".

The equivalent using a named function works as I expect:

  #include <iostream>
  #include <memory>
   
  void deleter(int *p) { std::cout << "deleter (" << *p << ")\n"; };
   
  int main()
  {
     int x = 42;
     std::unique_ptr<int, decltype(&deleter)> upi(&x, deleter);
  }

In this case it's an error to omit the second argument to the
constructor.  (I used decltype just to make the comparison clear but of
course one could simply write the type (void (*)(int *)) in the template
instead.)

I am pretty sure that the answer is in some way related to the fact that
C++ lambdas have unique types, but it's not clear to me what's really
going on.  Explanations welcome.  Pointers to explanatory text in the
standard even more so.

-- 
Ben.

[toc] | [next] | [standalone]


#117643

FromBo Persson <bo@bo-persson.se>
Date2023-11-07 12:12 +0100
Message-ID<kqukdqF1fdjU1@mid.individual.net>
In reply to#117642
On 2023-11-07 at 11:11, Ben Bacarisse wrote:
> This question got a bit lost in another thread, so I am re-posting it on
> its own.  What is decltype doing in the following short program?
> 
>    #include <iostream>
>    #include <memory>
>     
>    int main()
>    {
>       int x = 42;
>       std::unique_ptr<int, decltype([](int *p) {
>          std::cout << "delete (" << *p << ")\n";
>       })> upi(&x);
>    }
> 
> I expected decltype to supply just a type in the template and for a
> second argument to be required when constructing upi, but no.  The
> program compiles (gcc and clang) and prints "delete (42)".
> 
> The equivalent using a named function works as I expect:
> 
>    #include <iostream>
>    #include <memory>
>     
>    void deleter(int *p) { std::cout << "deleter (" << *p << ")\n"; };
>     
>    int main()
>    {
>       int x = 42;
>       std::unique_ptr<int, decltype(&deleter)> upi(&x, deleter);
>    }
> 
> In this case it's an error to omit the second argument to the
> constructor.  (I used decltype just to make the comparison clear but of
> course one could simply write the type (void (*)(int *)) in the template
> instead.)
> 
> I am pretty sure that the answer is in some way related to the fact that
> C++ lambdas have unique types, but it's not clear to me what's really
> going on.  Explanations welcome.  Pointers to explanatory text in the
> standard even more so.
> 

You *have* the explanation right there. :-)

The lambda has a unique type and can be default constructed, so that is 
what happens.

The function pointer needs a *real* function to point to, so requires a 
second parameter.

[toc] | [prev] | [next] | [standalone]


#117646

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2023-11-07 15:16 +0000
Message-ID<87wmut386d.fsf@bsb.me.uk>
In reply to#117643
Bo Persson <bo@bo-persson.se> writes:

> On 2023-11-07 at 11:11, Ben Bacarisse wrote:
>> This question got a bit lost in another thread, so I am re-posting it on
>> its own.  What is decltype doing in the following short program?
>>    #include <iostream>
>>    #include <memory>
>>        int main()
>>    {
>>       int x = 42;
>>       std::unique_ptr<int, decltype([](int *p) {
>>          std::cout << "delete (" << *p << ")\n";
>>       })> upi(&x);
>>    }
>> I expected decltype to supply just a type in the template and for a
>> second argument to be required when constructing upi, but no.  The
>> program compiles (gcc and clang) and prints "delete (42)".
>> The equivalent using a named function works as I expect:
>>    #include <iostream>
>>    #include <memory>
>>        void deleter(int *p) { std::cout << "deleter (" << *p << ")\n"; };
>>        int main()
>>    {
>>       int x = 42;
>>       std::unique_ptr<int, decltype(&deleter)> upi(&x, deleter);
>>    }
>> In this case it's an error to omit the second argument to the
>> constructor.  (I used decltype just to make the comparison clear but of
>> course one could simply write the type (void (*)(int *)) in the template
>> instead.)
>> I am pretty sure that the answer is in some way related to the fact that
>> C++ lambdas have unique types, but it's not clear to me what's really
>> going on.  Explanations welcome.  Pointers to explanatory text in the
>> standard even more so.
>
> You *have* the explanation right there. :-)
>
> The lambda has a unique type and can be default constructed, so that is
> what happens.

Ah.  OK.  It seems a little mysterious, but it's obvious when you see
it.  Here's a more direct example:

  #include <iostream>

  int main()
  {
     decltype([](int i){std::cout << "i=" << i << "\n";}) f;
     f(42);
  }

Thank you.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#117647

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-11-07 16:22 +0100
Message-ID<uidko0$12aqc$1@raubtier-asyl.eternal-september.org>
In reply to#117646
Am 07.11.2023 um 16:16 schrieb Ben Bacarisse:

> Ah.  OK.  It seems a little mysterious, but it's obvious when you see
> it.  Here's a more direct example:
> 
>    #include <iostream>
> 
>    int main()
>    {
>       decltype([](int i){std::cout << "i=" << i << "\n";}) f;
>       f(42);
>    }

Put a + before the [] and you have unefined behaviour. ;-)

[toc] | [prev] | [next] | [standalone]


#117644

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-11-07 13:23 +0100
Message-ID<0754fa6f-3628-4ea1-be00-0e4ce9623add@gmail.com>
In reply to#117642
The lambda *has* a calling operator, but isn't a calling operator.
The lambda itself is the []-object in this case. If you don't have
any captures you can instantiate the lambda withoug copying or mov-
ing. So if I handle the lambda's type implicitly to the constructor
of unique_ptr with a default-parameter it's default-constructed.
Deducing the lambda's type with decltype is possible only with
C++20 as C++20 allows using a lambda in an unevaluated context;
default-constructing a non-capturing lambda without copying is
AFAIK also C++20.
I can recommend the E-Book "C++ Lambda Story" from Leanpub for that.
It's in this C++ book collection:
https://mega.nz/file/es1gBajQ#nlMLfzK5d3MWoPU8K0B7gf4JyZoTFgWt1hJMJFtSP0I
Password for the archive is "Usenet".


Am 07.11.2023 um 11:11 schrieb Ben Bacarisse:
> This question got a bit lost in another thread, so I am re-posting it on
> its own.  What is decltype doing in the following short program?
> 
>    #include <iostream>
>    #include <memory>
>     
>    int main()
>    {
>       int x = 42;
>       std::unique_ptr<int, decltype([](int *p) {
>          std::cout << "delete (" << *p << ")\n";
>       })> upi(&x);
>    }
> 
> I expected decltype to supply just a type in the template and for a
> second argument to be required when constructing upi, but no.  The
> program compiles (gcc and clang) and prints "delete (42)".
> 
> The equivalent using a named function works as I expect:
> 
>    #include <iostream>
>    #include <memory>
>     
>    void deleter(int *p) { std::cout << "deleter (" << *p << ")\n"; };
>     
>    int main()
>    {
>       int x = 42;
>       std::unique_ptr<int, decltype(&deleter)> upi(&x, deleter);
>    }
> 
> In this case it's an error to omit the second argument to the
> constructor.  (I used decltype just to make the comparison clear but of
> course one could simply write the type (void (*)(int *)) in the template
> instead.)
> 
> I am pretty sure that the answer is in some way related to the fact that
> C++ lambdas have unique types, but it's not clear to me what's really
> going on.  Explanations welcome.  Pointers to explanatory text in the
> standard even more so.
> 

[toc] | [prev] | [next] | [standalone]


#117645

FromDavid Brown <david.brown@hesbynett.no>
Date2023-11-07 15:38 +0100
Message-ID<uidi4g$11q5c$1@dont-email.me>
In reply to#117644
On 07/11/2023 13:23, Bonita Montero wrote:
> I can recommend the E-Book "C++ Lambda Story" from Leanpub for that.
> It's in this C++ book collection:
> <--->
> Password for the archive is "<--->".

I have reported you to the author for encouraging copyright 
infringement.  It is disgraceful that you would post such links in 
public.  You claim to be a professional developer, yet encourage 
stealing a $15 book from a guy who publishes an enormous amount of free 
information on his blog.

It is now up to the author to decide where to take this.  Hopefully at a 
minimum the mega.nz file will be removed, and the account deleted.

[toc] | [prev] | [next] | [standalone]


#117648

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-11-07 16:38 +0100
Message-ID<uidlm9$12g63$1@raubtier-asyl.eternal-september.org>
In reply to#117645
Am 07.11.2023 um 15:38 schrieb David Brown:

> I have reported you to the author for encouraging copyright 
> infringement.  It is disgraceful that you would post such links in 
> public.  You claim to be a professional developer, yet encourage 
> stealing a $15 book from a guy who publishes an enormous amount of free 
> information on his blog.
> It is now up to the author to decide where to take this.  Hopefully at a 
> minimum the mega.nz file will be removed, and the account deleted.

It's like with muscians; those people make their money with concerts
and not with sold MP3s and CDs. And people writing these books earn
their money with courses. In the collection I've shown there are
several books from Rainer Grimm; his courses are about 1.500$ a day.

[toc] | [prev] | [next] | [standalone]


#117649

Fromscott@slp53.sl.home (Scott Lurndal)
Date2023-11-07 16:04 +0000
Message-ID<Det2N.217705$0UVe.113634@fx17.iad>
In reply to#117648
Bonita Montero <Bonita.Montero@gmail.com> writes:
>Am 07.11.2023 um 15:38 schrieb David Brown:
>
>> I have reported you to the author for encouraging copyright 
>> infringement.  It is disgraceful that you would post such links in 
>> public.  You claim to be a professional developer, yet encourage 
>> stealing a $15 book from a guy who publishes an enormous amount of free 
>> information on his blog.
>> It is now up to the author to decide where to take this.  Hopefully at a 
>> minimum the mega.nz file will be removed, and the account deleted.
>
>It's like with muscians; those people make their money with concerts
>and not with sold MP3s and CDs.

You are incorrect with respect to musicians.

> And people writing these books earn their money with courses.

You are incorrect with respect to authors.

Real people pay for what they get, they don't steal it.

[toc] | [prev] | [next] | [standalone]


#117650

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-11-07 17:16 +0100
Message-ID<uidnt1$12vup$1@raubtier-asyl.eternal-september.org>
In reply to#117649
Am 07.11.2023 um 17:04 schrieb Scott Lurndal:
> Bonita Montero <Bonita.Montero@gmail.com> writes:
>> Am 07.11.2023 um 15:38 schrieb David Brown:
>>
>>> I have reported you to the author for encouraging copyright
>>> infringement.  It is disgraceful that you would post such links in
>>> public.  You claim to be a professional developer, yet encourage
>>> stealing a $15 book from a guy who publishes an enormous amount of free
>>> information on his blog.
>>> It is now up to the author to decide where to take this.  Hopefully at a
>>> minimum the mega.nz file will be removed, and the account deleted.
>>
>> It's like with muscians; those people make their money with concerts
>> and not with sold MP3s and CDs.
> 
> You are incorrect with respect to musicians.

Absolutely not.

>> And people writing these books earn their money with courses.

> You are incorrect with respect to authors.

At least not for those which actually teaching in courses.

[toc] | [prev] | [next] | [standalone]


#117651

FromDavid Brown <david.brown@hesbynett.no>
Date2023-11-07 17:45 +0100
Message-ID<uidpio$13asg$1@dont-email.me>
In reply to#117648
On 07/11/2023 16:38, Bonita Montero wrote:
> Am 07.11.2023 um 15:38 schrieb David Brown:
> 
>> I have reported you to the author for encouraging copyright 
>> infringement.  It is disgraceful that you would post such links in 
>> public.  You claim to be a professional developer, yet encourage 
>> stealing a $15 book from a guy who publishes an enormous amount of 
>> free information on his blog.
>> It is now up to the author to decide where to take this.  Hopefully at 
>> a minimum the mega.nz file will be removed, and the account deleted.
> 
> It's like with muscians; those people make their money with concerts
> and not with sold MP3s and CDs. And people writing these books earn
> their money with courses. In the collection I've shown there are
> several books from Rainer Grimm; his courses are about 1.500$ a day.
> 

Have you asked the author of the book - not Rainer Grimm - if he is 
happy for people to make unlicensed copies of his book, and encourage 
others to download such copies?  I'd be very surprised to hear that - 
just as none of the professional musicians I know are at all happy to 
have people make unlicensed copies of their music.

Have you the faintest idea how hard it is for people to make a living by 
giving courses?  Or concerts?  Baring a few of the most famous 
performers, musicians rarely much profit from concerts.

And regardless of how much you, in your complete ignorance, think people 
make from books, courses, or anything else - it is not /your/ place to 
decide that this author should not be getting paid fairly for his book.

[toc] | [prev] | [next] | [standalone]


#117652

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-11-07 17:49 +0100
Message-ID<uidpqn$13cal$1@raubtier-asyl.eternal-september.org>
In reply to#117651
Am 07.11.2023 um 17:45 schrieb David Brown:

> Have you asked the author of the book - not Rainer Grimm - if he is 
> happy for people to make unlicensed copies of his book, and encourage 
> others to download such copies?  ...

He's for sure not happy, but not really hurt if he gives courses
at this price.

[toc] | [prev] | [next] | [standalone]


#117655

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-11-07 11:59 -0800
Message-ID<uie4v6$15j47$3@dont-email.me>
In reply to#117652
On 11/7/2023 8:49 AM, Bonita Montero wrote:
> Am 07.11.2023 um 17:45 schrieb David Brown:
> 
>> Have you asked the author of the book - not Rainer Grimm - if he is 
>> happy for people to make unlicensed copies of his book, and encourage 
>> others to download such copies?  ...
> 
> He's for sure not happy, but not really hurt if he gives courses
> at this price.

Wow! You are a thief as well... Humm... I know you don't know very much 
about synchronization primitives and multi-threading in general, but a 
thief to boot? wow.

[toc] | [prev] | [next] | [standalone]


#117656

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-11-08 07:28 +0100
Message-ID<uif9ph$1foq6$1@raubtier-asyl.eternal-september.org>
In reply to#117655
Am 07.11.2023 um 20:59 schrieb Chris M. Thomasson:

> Wow! You are a thief as well... Humm... I know you don't know very much 
> about synchronization primitives and multi-threading in general, ...

You're a child.

[toc] | [prev] | [next] | [standalone]


#117666

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-11-08 13:52 -0800
Message-ID<uigvvp$1q2i4$3@dont-email.me>
In reply to#117656
On 11/7/2023 10:28 PM, Bonita Montero wrote:
> Am 07.11.2023 um 20:59 schrieb Chris M. Thomasson:
> 
>> Wow! You are a thief as well... Humm... I know you don't know very 
>> much about synchronization primitives and multi-threading in general, ...
> 
> You're a child.
> 
> 

Humm... Apparently you are an adult that steals! Wow. Did you happen to 
see all of those parents who stole candy from the front porch of 
somebody who left out a lot of candy? Wow. Those lowlife scum bags:

https://youtu.be/AFjx1xnGxD8

God damn assholes!

[toc] | [prev] | [next] | [standalone]


#117657

FromDavid Brown <david.brown@hesbynett.no>
Date2023-11-08 09:45 +0100
Message-ID<uifhs0$1h42v$1@dont-email.me>
In reply to#117645
On 07/11/2023 15:38, David Brown wrote:
> On 07/11/2023 13:23, Bonita Montero wrote:
>> I can recommend the E-Book "C++ Lambda Story" from Leanpub for that.
>> It's in this C++ book collection:
>> <--->
>> Password for the archive is "<--->".
> 
> I have reported you to the author for encouraging copyright 
> infringement.  It is disgraceful that you would post such links in 
> public.  You claim to be a professional developer, yet encourage 
> stealing a $15 book from a guy who publishes an enormous amount of free 
> information on his blog.
> 
> It is now up to the author to decide where to take this.  Hopefully at a 
> minimum the mega.nz file will be removed, and the account deleted.
> 

For those that are interested, here is the real book:

<https://leanpub.com/cpplambda>


[toc] | [prev] | [next] | [standalone]


#117658

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-11-08 10:01 +0100
Message-ID<uifip5$1ha8b$1@raubtier-asyl.eternal-september.org>
In reply to#117657
Am 08.11.2023 um 09:45 schrieb David Brown:
> On 07/11/2023 15:38, David Brown wrote:
>> On 07/11/2023 13:23, Bonita Montero wrote:
>>> I can recommend the E-Book "C++ Lambda Story" from Leanpub for that.
>>> It's in this C++ book collection:
>>> <--->
>>> Password for the archive is "<--->".
>>
>> I have reported you to the author for encouraging copyright 
>> infringement.  It is disgraceful that you would post such links in 
>> public.  You claim to be a professional developer, yet encourage 
>> stealing a $15 book from a guy who publishes an enormous amount of 
>> free information on his blog.
>>
>> It is now up to the author to decide where to take this.  Hopefully at 
>> a minimum the mega.nz file will be removed, and the account deleted.
>>
> 
> For those that are interested, here is the real book:
> 
> <https://leanpub.com/cpplambda>

You can also find it on annas-archive.org.

[toc] | [prev] | [next] | [standalone]


#117668

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-11-08 13:54 -0800
Message-ID<uih037$1q2i4$4@dont-email.me>
In reply to#117657
On 11/8/2023 12:45 AM, David Brown wrote:
> On 07/11/2023 15:38, David Brown wrote:
>> On 07/11/2023 13:23, Bonita Montero wrote:
>>> I can recommend the E-Book "C++ Lambda Story" from Leanpub for that.
>>> It's in this C++ book collection:
>>> <--->
>>> Password for the archive is "<--->".
>>
>> I have reported you to the author for encouraging copyright 
>> infringement.  It is disgraceful that you would post such links in 
>> public.  You claim to be a professional developer, yet encourage 
>> stealing a $15 book from a guy who publishes an enormous amount of 
>> free information on his blog.
>>
>> It is now up to the author to decide where to take this.  Hopefully at 
>> a minimum the mega.nz file will be removed, and the account deleted.
>>
> 
> For those that are interested, here is the real book:
> 
> <https://leanpub.com/cpplambda>

Indeed. Fwiw and afaict, Bonita just might be an actual piece of shit.

https://youtu.be/AFjx1xnGxD8

[toc] | [prev] | [next] | [standalone]


#117687

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-11-09 05:50 +0100
Message-ID<uihoe1$22127$1@raubtier-asyl.eternal-september.org>
In reply to#117668
Am 08.11.2023 um 22:54 schrieb Chris M. Thomasson:

> Indeed. Fwiw and afaict, Bonita just might be an actual piece of shit.
> https://youtu.be/AFjx1xnGxD8

You're really primitive.

[toc] | [prev] | [next] | [standalone]


#117690

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-11-08 20:55 -0800
Message-ID<uihoo6$21rab$8@dont-email.me>
In reply to#117687
On 11/8/2023 8:50 PM, Bonita Montero wrote:
> Am 08.11.2023 um 22:54 schrieb Chris M. Thomasson:
> 
>> Indeed. Fwiw and afaict, Bonita just might be an actual piece of shit.
>> https://youtu.be/AFjx1xnGxD8
> 
> You're really primitive.
> 
> 

You would steal the candy along with the books, right? Or, am I wrong here?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web