Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #117644
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: What's decltype doing here? |
| Date | 2023-11-07 13:23 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <0754fa6f-3628-4ea1-be00-0e4ce9623add@gmail.com> (permalink) |
| References | <878r7950vy.fsf@bsb.me.uk> |
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.
>
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web