Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #117643
| From | Bo Persson <bo@bo-persson.se> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: What's decltype doing here? |
| Date | 2023-11-07 12:12 +0100 |
| Message-ID | <kqukdqF1fdjU1@mid.individual.net> (permalink) |
| References | <878r7950vy.fsf@bsb.me.uk> |
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.
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