Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #86257 > unrolled thread

Using local variables in C callbacks

Started byBonita Montero <Bonita.Montero@gmail.com>
First post2022-09-11 17:18 +0200
Last post2022-09-15 13:29 -0700
Articles 20 on this page of 35 — 10 participants

Back to article view | Back to comp.lang.c++


Contents

  Using local variables in C callbacks Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-11 17:18 +0200
    Re: Using local variables in C callbacks Muttley@dastardlyhq.com - 2022-09-11 15:45 +0000
      Re: Using local variables in C callbacks Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-11 17:51 +0200
        Re: Using local variables in C callbacks Muttley@dastardlyhq.com - 2022-09-11 15:53 +0000
          Re: Using local variables in C callbacks Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-11 17:57 +0200
            Re: Using local variables in C callbacks Tony Oliver <guinness.tony@gmail.com> - 2022-09-12 17:05 -0700
              Re: Using local variables in C callbacks Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-13 07:18 +0200
    Re: Using local variables in C callbacks Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-12 07:02 +0200
    Re: Using local variables in C callbacks Juha Nieminen <nospam@thanks.invalid> - 2022-09-12 06:03 +0000
      Re: Using local variables in C callbacks Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-12 09:16 +0200
        Re: Using local variables in C callbacks Juha Nieminen <nospam@thanks.invalid> - 2022-09-13 06:52 +0000
          Re: Using local variables in C callbacks Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-13 12:09 +0200
            Re: Using local variables in C callbacks Juha Nieminen <nospam@thanks.invalid> - 2022-09-13 12:57 +0000
              Re: Using local variables in C callbacks Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-13 15:57 +0200
                Re: Using local variables in C callbacks Juha Nieminen <nospam@thanks.invalid> - 2022-09-14 06:08 +0000
                  Re: Using local variables in C callbacks Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-14 09:00 +0200
                    Re: Using local variables in C callbacks Juha Nieminen <nospam@thanks.invalid> - 2022-09-14 12:26 +0000
                      Re: Using local variables in C callbacks Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-14 14:59 +0200
                        Re: Using local variables in C callbacks David Brown <david.brown@hesbynett.no> - 2022-09-14 16:01 +0200
                          Re: Using local variables in C callbacks Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-14 16:49 +0200
                        Re: Using local variables in C callbacks Juha Nieminen <nospam@thanks.invalid> - 2022-09-15 05:35 +0000
                          Re: Using local variables in C callbacks Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-15 08:26 +0200
                            Re: Using local variables in C callbacks scott@slp53.sl.home (Scott Lurndal) - 2022-09-15 14:09 +0000
                              Re: Using local variables in C callbacks Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-15 16:56 +0200
                          Re: Using local variables in C callbacks Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-09-15 05:43 -0700
                            Re: Using local variables in C callbacks Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-15 16:02 +0200
                            Re: Using local variables in C callbacks Juha Nieminen <nospam@thanks.invalid> - 2022-09-16 06:08 +0000
                              Re: Using local variables in C callbacks Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-16 13:43 +0200
                              Re: Using local variables in C callbacks Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-09-16 06:57 -0700
                              Re: Using local variables in C callbacks Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-09-16 10:13 -0700
                    Re: Using local variables in C callbacks "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-09-14 15:38 -0700
            Re: Using local variables in C callbacks Öö Tiib <ootiib@hot.ee> - 2022-09-13 22:20 -0700
          Re: Using local variables in C callbacks "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-09-13 12:48 -0700
            Re: Using local variables in C callbacks Juha Nieminen <nospam@thanks.invalid> - 2022-09-14 06:09 +0000
              Re: Using local variables in C callbacks "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-09-15 13:29 -0700

Page 1 of 2  [1] 2  Next page →


#86257 — Using local variables in C callbacks

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-09-11 17:18 +0200
SubjectUsing local variables in C callbacks
Message-ID<tfku77$1vir2$1@dont-email.me>
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;
}

[toc] | [next] | [standalone]


#86259

FromMuttley@dastardlyhq.com
Date2022-09-11 15:45 +0000
Message-ID<tfkvpr$l7i$1@gioia.aioe.org>
In reply to#86257
On Sun, 11 Sep 2022 17:18:13 +0200
Bonita Montero <Bonita.Montero@gmail.com> wrote:
>What do you think about this ?

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

[toc] | [prev] | [next] | [standalone]


#86260

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-09-11 17:51 +0200
Message-ID<tfl05k$1vt01$1@dont-email.me>
In reply to#86259
Am 11.09.2022 um 17:45 schrieb Muttley@dastardlyhq.com:
> On Sun, 11 Sep 2022 17:18:13 +0200
> Bonita Montero <Bonita.Montero@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++.


[toc] | [prev] | [next] | [standalone]


#86261

FromMuttley@dastardlyhq.com
Date2022-09-11 15:53 +0000
Message-ID<tfl09q$t09$1@gioia.aioe.org>
In reply to#86260
On Sun, 11 Sep 2022 17:51:30 +0200
Bonita Montero <Bonita.Montero@gmail.com> wrote:
>Am 11.09.2022 um 17:45 schrieb Muttley@dastardlyhq.com:
>> On Sun, 11 Sep 2022 17:18:13 +0200
>> Bonita Montero <Bonita.Montero@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.

[toc] | [prev] | [next] | [standalone]


#86263

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-09-11 17:57 +0200
Message-ID<tfl0h3$1vt01$3@dont-email.me>
In reply to#86261
Am 11.09.2022 um 17:53 schrieb Muttley@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.

[toc] | [prev] | [next] | [standalone]


#86292

FromTony Oliver <guinness.tony@gmail.com>
Date2022-09-12 17:05 -0700
Message-ID<544b841d-0f5d-4ea7-9686-fb43c0395f60n@googlegroups.com>
In reply to#86263
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.

[toc] | [prev] | [next] | [standalone]


#86293

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-09-13 07:18 +0200
Message-ID<tfp3qe$2gpt6$1@dont-email.me>
In reply to#86292
Am 13.09.2022 um 02:05 schrieb Tony Oliver:
> 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.

That's not boast at all.

[toc] | [prev] | [next] | [standalone]


#86273

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-09-12 07:02 +0200
Message-ID<tfmehi$26epq$1@dont-email.me>
In reply to#86257
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.

[toc] | [prev] | [next] | [standalone]


#86275

FromJuha Nieminen <nospam@thanks.invalid>
Date2022-09-12 06:03 +0000
Message-ID<tfmi2j$1bua$1@gioia.aioe.org>
In reply to#86257
Bonita Montero <Bonita.Montero@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.

[toc] | [prev] | [next] | [standalone]


#86276

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-09-12 09:16 +0200
Message-ID<tfmmbd$272cp$1@dont-email.me>
In reply to#86275
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.

[toc] | [prev] | [next] | [standalone]


#86295

FromJuha Nieminen <nospam@thanks.invalid>
Date2022-09-13 06:52 +0000
Message-ID<tfp9b1$dv$2@gioia.aioe.org>
In reply to#86276
Bonita Montero <Bonita.Montero@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.

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

[toc] | [prev] | [next] | [standalone]


#86296

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-09-13 12:09 +0200
Message-ID<tfpks1$2i9ke$1@dont-email.me>
In reply to#86295
Am 13.09.2022 um 08:52 schrieb Juha Nieminen:
> Bonita Montero <Bonita.Montero@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.

[toc] | [prev] | [next] | [standalone]


#86297

FromJuha Nieminen <nospam@thanks.invalid>
Date2022-09-13 12:57 +0000
Message-ID<tfpunq$ahv$1@gioia.aioe.org>
In reply to#86296
Bonita Montero <Bonita.Montero@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.

[toc] | [prev] | [next] | [standalone]


#86300

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-09-13 15:57 +0200
Message-ID<tfq286$2jho5$1@dont-email.me>
In reply to#86297
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.

[toc] | [prev] | [next] | [standalone]


#86306

FromJuha Nieminen <nospam@thanks.invalid>
Date2022-09-14 06:08 +0000
Message-ID<tfrr42$1ges$2@gioia.aioe.org>
In reply to#86300
Bonita Montero <Bonita.Montero@gmail.com> wrote:
> 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.

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.

[toc] | [prev] | [next] | [standalone]


#86309

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-09-14 09:00 +0200
Message-ID<tfru54$2rhfm$1@dont-email.me>
In reply to#86306
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.

[toc] | [prev] | [next] | [standalone]


#86320

FromJuha Nieminen <nospam@thanks.invalid>
Date2022-09-14 12:26 +0000
Message-ID<tfsh97$1vug$2@gioia.aioe.org>
In reply to#86309
Bonita Montero <Bonita.Montero@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?

[toc] | [prev] | [next] | [standalone]


#86321

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-09-14 14:59 +0200
Message-ID<tfsj6s$2uvaj$4@dont-email.me>
In reply to#86320
Am 14.09.2022 um 14:26 schrieb Juha Nieminen:
> Bonita Montero <Bonita.Montero@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.

> 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?

[toc] | [prev] | [next] | [standalone]


#86324

FromDavid Brown <david.brown@hesbynett.no>
Date2022-09-14 16:01 +0200
Message-ID<tfsmrh$2vqod$3@dont-email.me>
In reply to#86321
On 14/09/2022 14:59, Bonita Montero wrote:
> Am 14.09.2022 um 14:26 schrieb Juha Nieminen:
>> Bonita Montero <Bonita.Montero@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.

>> 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?
> 
> 

[toc] | [prev] | [next] | [standalone]


#86325

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-09-14 16:49 +0200
Message-ID<tfspkh$30fh5$1@dont-email.me>
In reply to#86324
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.Montero@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".

> 
>>> 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?
>>
>>
> 

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.c++


csiph-web