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.
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.
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 "<--->".
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);
}
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.
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.
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.
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.
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.
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? ...
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, ...
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.
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>
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.
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>
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.
Indeed. Fwiw and afaict, Bonita just might be an actual piece of shit. https://youtu.be/AFjx1xnGxD8
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 491 |
Nodes: | 16 (2 / 14) |
Uptime: | 145:07:49 |
Calls: | 9,694 |
Calls today: | 4 |
Files: | 13,730 |
Messages: | 6,178,504 |