Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What's decltype doing here?

96 views
Skip to first unread message

Ben Bacarisse

unread,
Nov 7, 2023, 5:11:34 AM11/7/23
to
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.

Bo Persson

unread,
Nov 7, 2023, 6:13:16 AM11/7/23
to
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.


Bonita Montero

unread,
Nov 7, 2023, 7:23:50 AM11/7/23
to
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".

David Brown

unread,
Nov 7, 2023, 9:38:26 AM11/7/23
to
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.


Ben Bacarisse

unread,
Nov 7, 2023, 10:17:02 AM11/7/23
to
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.

Bonita Montero

unread,
Nov 7, 2023, 10:22:57 AM11/7/23
to
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. ;-)

Bonita Montero

unread,
Nov 7, 2023, 10:39:07 AM11/7/23
to
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.

Scott Lurndal

unread,
Nov 7, 2023, 11:05:08 AM11/7/23
to
Bonita Montero <Bonita....@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.

Bonita Montero

unread,
Nov 7, 2023, 11:16:52 AM11/7/23
to
Am 07.11.2023 um 17:04 schrieb Scott Lurndal:
> Bonita Montero <Bonita....@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.


David Brown

unread,
Nov 7, 2023, 11:45:29 AM11/7/23
to
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.


Bonita Montero

unread,
Nov 7, 2023, 11:49:44 AM11/7/23
to
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.

Chris M. Thomasson

unread,
Nov 7, 2023, 2:59:52 PM11/7/23
to
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.

Bonita Montero

unread,
Nov 8, 2023, 1:28:20 AM11/8/23
to
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.


David Brown

unread,
Nov 8, 2023, 3:46:12 AM11/8/23
to
For those that are interested, here is the real book:

<https://leanpub.com/cpplambda>



Bonita Montero

unread,
Nov 8, 2023, 4:01:43 AM11/8/23
to
You can also find it on annas-archive.org.


Chris M. Thomasson

unread,
Nov 8, 2023, 10:29:06 PM11/8/23
to
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!

Chris M. Thomasson

unread,
Nov 8, 2023, 10:29:24 PM11/8/23
to
Indeed. Fwiw and afaict, Bonita just might be an actual piece of shit.

https://youtu.be/AFjx1xnGxD8

Bonita Montero

unread,
Nov 8, 2023, 11:51:10 PM11/8/23
to
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.


Chris M. Thomasson

unread,
Nov 8, 2023, 11:56:20 PM11/8/23
to
You would steal the candy along with the books, right? Or, am I wrong here?
0 new messages