• What's decltype doing here?

    From Ben Bacarisse@21:1/5 to All on Tue Nov 7 10:11:13 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bo Persson@21:1/5 to Ben Bacarisse on Tue Nov 7 12:12:58 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Tue Nov 7 13:23:32 2023
    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.


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Bonita Montero on Tue Nov 7 15:38:07 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Tue Nov 7 16:22:40 2023
    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. ;-)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Tue Nov 7 16:38:50 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Scott Lurndal@21:1/5 to Bonita Montero on Tue Nov 7 16:04:51 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Bo Persson on Tue Nov 7 15:16:42 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Bonita Montero on Tue Nov 7 17:45:11 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Tue Nov 7 17:16:33 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Tue Nov 7 17:49:27 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Tue Nov 7 11:59:35 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Wed Nov 8 07:28:01 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to David Brown on Wed Nov 8 09:45:52 2023
    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>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Wed Nov 8 10:01:25 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Wed Nov 8 13:52:57 2023
    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!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to David Brown on Wed Nov 8 13:54:47 2023
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris M. Thomasson@21:1/5 to Bonita Montero on Wed Nov 8 20:55:34 2023
    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?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bonita Montero@21:1/5 to All on Thu Nov 9 05:50:11 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)