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

Using local variables in C callbacks

52 views
Skip to first unread message

Bonita Montero

unread,
Sep 11, 2022, 11:18:17 AM9/11/22
to
What do you think about this ?

#include <iostream>
#include <vector>
#include <string>
#include <functional>
#include <link.h>

using namespace std;

int main()
{
vector<string> images;
auto fnDlIterate = bind(
[&]( dl_phdr_info *image, size_t size ) -> int
{
try
{
images.emplace_back( image->dlpi_name );
}
catch( bad_alloc const & )
{
return 1;
}
return 0;
}, placeholders::_1, placeholders::_2 );
using fn_callback_t = decltype(fnDlIterate);
if( dl_iterate_phdr(
[]( dl_phdr_info *image, size_t size, void *pFn ) -> int
{
return (*(fn_callback_t *)pFn)( image, size );
}, &fnDlIterate ) )
return EXIT_FAILURE;
for( string const &image : images )
cout << "\"" << image << "\"" << endl;
}

Mut...@dastardlyhq.com

unread,
Sep 11, 2022, 11:45:17 AM9/11/22
to
On Sun, 11 Sep 2022 17:18:13 +0200
Bonita Montero <Bonita....@gmail.com> wrote:
>What do you think about this ?

Enter it into the Obfuscated C++ contest. I'm sure it would do well.

Bonita Montero

unread,
Sep 11, 2022, 11:51:32 AM9/11/22
to
There's nothing obfuscated if you're used to program functional in C++.



Mut...@dastardlyhq.com

unread,
Sep 11, 2022, 11:53:46 AM9/11/22
to
On Sun, 11 Sep 2022 17:51:30 +0200
Bonita Montero <Bonita....@gmail.com> wrote:
>Am 11.09.2022 um 17:45 schrieb Mut...@dastardlyhq.com:
>> On Sun, 11 Sep 2022 17:18:13 +0200
>> Bonita Montero <Bonita....@gmail.com> wrote:
>>> What do you think about this ?
>>
>> Enter it into the Obfuscated C++ contest. I'm sure it would do well.
>
>There's nothing obfuscated if you're used to program functional in C++.

Any why would you do that? Use ML or Erlang if thats your thing.

Anyway, like all your code, its an overcomplicated mess.

Bonita Montero

unread,
Sep 11, 2022, 11:57:39 AM9/11/22
to
Am 11.09.2022 um 17:53 schrieb Mut...@dastardlyhq.com:

>>> Enter it into the Obfuscated C++ contest. I'm sure it would do well.

>> There's nothing obfuscated if you're used to program functional in C++.

> Any why would you do that? Use ML or Erlang if thats your thing.

C++ has the best of all worlds, functional programming _with_ state.

> Anyway, like all your code, its an overcomplicated mess.

I program like that every day and for me that's absolutely not
complicated. For the purpose I've shown that's too much effort,
but if you have more complex callbacks that's a real relief.


Bonita Montero

unread,
Sep 12, 2022, 1:03:00 AM9/12/22
to
It could be even easier:
For C-APIs that have a callback and a context-pointer you
simply could write a wrapper that is given a function-object.

#include <iostream>
#include <vector>
#include <string>
#include <link.h>
#include <climits>
#include <dlfcn.h>

using namespace std;

template<typename Fn>
requires requires( Fn fn ) { { fn( (dl_phdr_info *)nullptr, (size_t)0 )
} -> same_as<int>; }
int dlIteratePhdr( Fn fn )
{
return dl_iterate_phdr(
[]( dl_phdr_info *image, size_t size, void *pFn ) -> int
{
return (*(Fn *)pFn)( image, size );
}, &fn );
};

int main()
{
size_t nImages = 0;
dlIteratePhdr( [&]( dl_phdr_info *, size_t ) -> int { ++nImages; return
0; } );
vector<string> images;
images.reserve( nImages );
if( dlIteratePhdr(
[&]( dl_phdr_info *image, size_t size ) -> int
{
try
{
images.emplace_back( image->dlpi_name );
return 0;
}
catch( bad_alloc const & )
{
return 1;
}
} ) )
return EXIT_FAILURE;
for( string const &image : images )
cout << "\"" << image << "\"" << endl;
}

This makes these APIs conveniently usable as if they were
native C++ APIs.

Juha Nieminen

unread,
Sep 12, 2022, 2:03:17 AM9/12/22
to
Bonita Montero <Bonita....@gmail.com> wrote:
> using namespace std;
>
> int main()
> {
> vector<string> images;
> auto fnDlIterate = bind(
> [&]( dl_phdr_info *image, size_t size ) -> int
> {
> try
> {
> images.emplace_back( image->dlpi_name );
> }
> catch( bad_alloc const & )
> {
> return 1;
> }
> return 0;
> }, placeholders::_1, placeholders::_2 );
> using fn_callback_t = decltype(fnDlIterate);
> if( dl_iterate_phdr(
> []( dl_phdr_info *image, size_t size, void *pFn ) -> int
> {
> return (*(fn_callback_t *)pFn)( image, size );
> }, &fnDlIterate ) )
> return EXIT_FAILURE;

I think this demonstrates beautifully why you shouldn't use "using
namespace std;"

The code is full of obfuscated names and it's very difficult to see with
a visual scan which names are declared locally, which are from the
standard library, and which are from some third-party library.

Also, I thought that lambdas made std::bind() obsolete.

Bonita Montero

unread,
Sep 12, 2022, 3:16:14 AM9/12/22
to
Am 12.09.2022 um 08:03 schrieb Juha Nieminen:

> I think this demonstrates beautifully why you shouldn't use "using
> namespace std;"

Idiot.

> The code is full of obfuscated names and it's very difficult to see with
> a visual scan which names are declared locally, which are from the
> standard library, and which are from some third-party library.

There's nothing obfuscated with that, it's just not your taste.
You're a person that constantly feels uncertain and you need
some rituals to feel certain; people that don't adhere to your
rituals make you feel ucertain again.

Tony Oliver

unread,
Sep 12, 2022, 8:05:37 PM9/12/22
to
On Sunday, 11 September 2022 at 16:57:39 UTC+1, Bonita Montero wrote:
---< snip >---
> I program like that every day

That's not the boast you thought it was.

Bonita Montero

unread,
Sep 13, 2022, 1:18:25 AM9/13/22
to
That's not boast at all.


Juha Nieminen

unread,
Sep 13, 2022, 2:52:35 AM9/13/22
to
The programmer is the poorest person to evaluate the readability of his
own code. He needs the perspective of another programmer to know how
readable and understandable his code actually is.

I also find funny you talking about "rituals". What else is always
writing the "using namespace std;" boilerplate than pretty much
effectively a ritual?

Bonita Montero

unread,
Sep 13, 2022, 6:09:23 AM9/13/22
to
Am 13.09.2022 um 08:52 schrieb Juha Nieminen:
> Bonita Montero <Bonita....@gmail.com> wrote:
>> Am 12.09.2022 um 08:03 schrieb Juha Nieminen:
>>
>>> I think this demonstrates beautifully why you shouldn't use "using
>>> namespace std;"
>>
>> Idiot.
>>
>>> The code is full of obfuscated names and it's very difficult to see with
>>> a visual scan which names are declared locally, which are from the
>>> standard library, and which are from some third-party library.
>>
>> There's nothing obfuscated with that, it's just not your taste.
>> You're a person that constantly feels uncertain and you need
>> some rituals to feel certain; people that don't adhere to your
>> rituals make you feel ucertain again.
>
> The programmer is the poorest person to evaluate the readability of his
> own code. He needs the perspective of another programmer to know how
> readable and understandable his code actually is.

Maybe, but I won't assess my code by idiots.

> I also find funny you talking about "rituals". What else is always
> writing the "using namespace std;" boilerplate than pretty much
> effectively a ritual?

If you're overburdened by such simple
things - better don't program at all.


Juha Nieminen

unread,
Sep 13, 2022, 8:57:51 AM9/13/22
to
Bonita Montero <Bonita....@gmail.com> wrote:
>> The programmer is the poorest person to evaluate the readability of his
>> own code. He needs the perspective of another programmer to know how
>> readable and understandable his code actually is.
>
> Maybe, but I won't assess my code by idiots.

Isn't it funny that I don't need to resort to any sort of insult or
childish namecalling, and I still trigger you?

>> I also find funny you talking about "rituals". What else is always
>> writing the "using namespace std;" boilerplate than pretty much
>> effectively a ritual?
>
> If you're overburdened by such simple
> things - better don't program at all.

Says the person who seems so badly overburdened by having to write some
prefixes that you avoid them like the plague and always write that
"using" magic spell at the beginning of every single source file like
your life depended on it, and ferociously attack anybody who questions
your decision.

One has to question why you are so adamant and aggressively defensive
about it.

Bonita Montero

unread,
Sep 13, 2022, 9:57:43 AM9/13/22
to
Am 13.09.2022 um 14:57 schrieb Juha Nieminen:

> Says the person who seems so badly overburdened by having to write some
> prefixes that you avoid them like the plague and always write that
> "using" magic spell at the beginning of every single source file like
> your life depended on it, and ferociously attack anybody who questions
> your decision.

I do that not because I'm overburdened but just because that's my style.

I don't know where the problem is for you here. This forum is
a leisure activity and not a community project. Everyone can
live out their prferences. The fact that you think you have
to proselytize for your style seems seriously mentally
disturbed. You probably need psychotherapy.


Chris M. Thomasson

unread,
Sep 13, 2022, 3:48:29 PM9/13/22
to
On 9/12/2022 11:52 PM, Juha Nieminen wrote:
> Bonita Montero <Bonita....@gmail.com> wrote:
>> Am 12.09.2022 um 08:03 schrieb Juha Nieminen:
>>
>>> I think this demonstrates beautifully why you shouldn't use "using
>>> namespace std;"
>>
>> Idiot.
>>
>>> The code is full of obfuscated names and it's very difficult to see with
>>> a visual scan which names are declared locally, which are from the
>>> standard library, and which are from some third-party library.
>>
>> There's nothing obfuscated with that, it's just not your taste.
>> You're a person that constantly feels uncertain and you need
>> some rituals to feel certain; people that don't adhere to your
>> rituals make you feel ucertain again.
>



> The programmer is the poorest person to evaluate the readability of his
> own code.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Do you mind if I quote that? I will give proper attributions of course,
Juha. :^)

Öö Tiib

unread,
Sep 14, 2022, 1:20:50 AM9/14/22
to
On Tuesday, 13 September 2022 at 13:09:23 UTC+3, Bonita Montero wrote:
> Am 13.09.2022 um 08:52 schrieb Juha Nieminen:
> > Bonita Montero <Bonita....@gmail.com> wrote:
> >> Am 12.09.2022 um 08:03 schrieb Juha Nieminen:
> >>
> >>> I think this demonstrates beautifully why you shouldn't use "using
> >>> namespace std;"
> >>
> >> Idiot.
> >>
> >>> The code is full of obfuscated names and it's very difficult to see with
> >>> a visual scan which names are declared locally, which are from the
> >>> standard library, and which are from some third-party library.
> >>
> >> There's nothing obfuscated with that, it's just not your taste.
> >> You're a person that constantly feels uncertain and you need
> >> some rituals to feel certain; people that don't adhere to your
> >> rituals make you feel ucertain again.
> >
> > The programmer is the poorest person to evaluate the readability of his
> > own code. He needs the perspective of another programmer to know how
> > readable and understandable his code actually is.
>
> Maybe, but I won't assess my code by idiots.

It takes one to know one.

Juha Nieminen

unread,
Sep 14, 2022, 2:08:22 AM9/14/22
to
When you post questions or code for others to review and consider,
you should make that code as easy to read and understand as possible.
When writing code for other people to see, you should be writing the
code for them, not for you. It doesn't matter so much that *you* can
understand your own code. It's important that *others* understand
easily your code.

It's a bit like when writing prose: The responsibility for the
understandability of the text is on the writer, not the reader.

Juha Nieminen

unread,
Sep 14, 2022, 2:10:04 AM9/14/22
to
Chris M. Thomasson <chris.m.t...@gmail.com> wrote:
>> The programmer is the poorest person to evaluate the readability of his
>> own code.
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> Do you mind if I quote that? I will give proper attributions of course,
> Juha. :^)

No need to attribute.

Bonita Montero

unread,
Sep 14, 2022, 3:00:04 AM9/14/22
to
Am 14.09.2022 um 08:08 schrieb Juha Nieminen:

> When you post questions or code for others to review and consider,
> you should make that code as easy to read and understand as possible.

The code is easy to read and if not you're mentally disoredered.
If you don't like my code, don't read it.


Juha Nieminen

unread,
Sep 14, 2022, 8:26:33 AM9/14/22
to
Bonita Montero <Bonita....@gmail.com> wrote:
> If you don't like my code, don't read it.

And that's *exactly* your problem. You are expecting others to do
the work of understanding your code rather than the other way
around, ie. doing some work to make your code understandable to
others.

If you want feedback, comments and answers, you should do the work
to make it as easy as possible for others to understand what you
have done. Expecting others to decipher your code is just selfish.

Are you honestly incapable of comprehending this?

Bonita Montero

unread,
Sep 14, 2022, 8:59:26 AM9/14/22
to
Am 14.09.2022 um 14:26 schrieb Juha Nieminen:
> Bonita Montero <Bonita....@gmail.com> wrote:
>> If you don't like my code, don't read it.
>
> And that's *exactly* your problem. You are expecting others to do
> the work of understanding your code rather than the other way
> around, ie. doing some work to make your code understandable to
> others.

Most people don't have your problem because they're not disordered
like you. If you don't like my code don't read it.

David Brown

unread,
Sep 14, 2022, 10:01:38 AM9/14/22
to
On 14/09/2022 14:59, Bonita Montero wrote:
> Am 14.09.2022 um 14:26 schrieb Juha Nieminen:
>> Bonita Montero <Bonita....@gmail.com> wrote:
>>> If you don't like my code, don't read it.
>>
>> And that's *exactly* your problem. You are expecting others to do
>> the work of understanding your code rather than the other way
>> around, ie. doing some work to make your code understandable to
>> others.
>
> Most people don't have your problem because they're not disordered
> like you. If you don't like my code don't read it.
>

And that is what happens - most people don't read it. It doesn't help
that after posting some code, you almost invariably make follow-up posts
correcting it or changing it. This all significantly limits the scope
for feedback or comments.

Bonita Montero

unread,
Sep 14, 2022, 10:49:06 AM9/14/22
to
Am 14.09.2022 um 16:01 schrieb David Brown:
> On 14/09/2022 14:59, Bonita Montero wrote:
>> Am 14.09.2022 um 14:26 schrieb Juha Nieminen:
>>> Bonita Montero <Bonita....@gmail.com> wrote:
>>>> If you don't like my code, don't read it.
>>>
>>> And that's *exactly* your problem. You are expecting others to do
>>> the work of understanding your code rather than the other way
>>> around, ie. doing some work to make your code understandable to
>>> others.
>>
>> Most people don't have your problem because they're not disordered
>> like you. If you don't like my code don't read it.
>>
>
> And that is what happens - most people don't read it. ...

For sure not because I'm "using using namespace std".

Chris M. Thomasson

unread,
Sep 14, 2022, 6:38:24 PM9/14/22
to
On 9/14/2022 12:00 AM, Bonita Montero wrote:
> Am 14.09.2022 um 08:08 schrieb Juha Nieminen:
>
>> When you post questions or code for others to review and consider,
>> you should make that code as easy to read and understand as possible.
>
> The code is easy to read and if not you're mentally disoredered.

I can read your code.


> If you don't like my code, don't read it.

Okay! :^)

Juha Nieminen

unread,
Sep 15, 2022, 1:35:32 AM9/15/22
to
Bonita Montero <Bonita....@gmail.com> wrote:
> Am 14.09.2022 um 14:26 schrieb Juha Nieminen:
>> Bonita Montero <Bonita....@gmail.com> wrote:
>>> If you don't like my code, don't read it.
>>
>> And that's *exactly* your problem. You are expecting others to do
>> the work of understanding your code rather than the other way
>> around, ie. doing some work to make your code understandable to
>> others.
>
> Most people don't have your problem because they're not disordered
> like you. If you don't like my code don't read it.

You just love throwing insults at people who critique your code,
don't you?

I have said this many times before, and I'll say it again: I have no
idea why people here still keep taking you seriously and responding
to your every post. You do nothing but insult people who disagree
with your way of doing things.

Bonita Montero

unread,
Sep 15, 2022, 2:26:41 AM9/15/22
to
Am 15.09.2022 um 07:35 schrieb Juha Nieminen:

>> Most people don't have your problem because they're not disordered
>> like you. If you don't like my code don't read it.

> You just love throwing insults at people who critique your code,
> don't you?

You don't want to make factual criticism of my code, you want me to
follow your style. And that's where it's appropriate to say what the
problem is, which is that you have a mental disorder.



Tim Rentsch

unread,
Sep 15, 2022, 8:43:34 AM9/15/22
to
What is it that you are hoping to accomplish by writing these
responses? If you have no idea why people keep responding to
his (or her) posts, why do you continue to respond to them?

Bonita Montero

unread,
Sep 15, 2022, 10:02:38 AM9/15/22
to
He is obsessed.

Scott Lurndal

unread,
Sep 15, 2022, 10:09:44 AM9/15/22
to
Criticism of your propensity for complexity is perfectly appropriate.

K.I.S.S.

Bonita Montero

unread,
Sep 15, 2022, 10:56:52 AM9/15/22
to
My code isn't complex but only has a differnt style.
I use a lot of functional programming to have sorter
or more performant code. This style is just uncommon.



Chris M. Thomasson

unread,
Sep 15, 2022, 4:29:28 PM9/15/22
to
:^)

Juha Nieminen

unread,
Sep 16, 2022, 2:08:41 AM9/16/22
to
Tim Rentsch <tr.1...@z991.linuxsc.com> wrote:
>> I have said this many times before, and I'll say it again: I have no
>> idea why people here still keep taking you seriously and responding
>> to your every post. You do nothing but insult people who disagree
>> with your way of doing things.
>
> What is it that you are hoping to accomplish by writing these
> responses? If you have no idea why people keep responding to
> his (or her) posts, why do you continue to respond to them?

If nobody else replied to her posts and just ignored her, I wouldn't
reply to them either.

Bonita Montero

unread,
Sep 16, 2022, 7:43:29 AM9/16/22
to
You don't write because of others, you write because you're obsessed.


Tim Rentsch

unread,
Sep 16, 2022, 9:57:57 AM9/16/22
to
This statement doesn't address either of my questions.

Keith Thompson

unread,
Sep 16, 2022, 1:14:10 PM9/16/22
to
How does other people replying to her posts compel you to do so?

Assuming you agree that the ideal solution (short of her either going
away or becoming reasonable) would be for everyone to ignore her, why do
you not want to help bring that about?

A lot of us never see here posts in the first place.

--
Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */
0 new messages