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

Lamda expression as "with"-block

100 views
Skip to first unread message

Alf P. Steinbach

unread,
May 5, 2016, 9:32:58 PM5/5/16
to
I just now found occasion to use a lambda expression as a "with"-block:

Impl( const Point size )
: text_buffer_()
, info_( text_buffer_.handle )
, text_buffer_activation_( text_buffer_.handle )
{
[&](){
Console_info params = info_.api_info();

params.dwSize = {short( size.x ), short( size.y )};
params.dwCursorPosition = {};
//screen.wAttributes = bitsForBgFg(
params.srWindow = {0, 0, short( size.x - 1 ), short( size.y
- 1 )};
params.dwMaximumWindowSize = params.dwSize;
info_.make_real( params );
}();
impl::winapi::set_output_mode(
text_buffer_.handle, impl::winapi::Output_flags::Enum()
);
}

(The Hungarian notation names are Microsoft's, not mine. Side note: what
does one call something when Microsoft calls it "info"? In this case
it's a set of values that specify the properties of a console.)

Yes, it's a far cry from a dedicated syntax, but still it gets the job done.

At one time in the C++03 days I created an awesome WITH macro based on
"for" I think it was, but I never used it more than the original single
use case...


Cheers,

- Alf

Alf P. Steinbach

unread,
May 6, 2016, 6:41:48 AM5/6/16
to
On 06.05.2016 03:57, Stefan Ram wrote:
> "Alf P. Steinbach" <alf.p.stein...@gmail.com> writes:
>> I just now found occasion to use a lambda expression as a "with"-block:
>> Impl( const Point size )
>> : text_buffer_()
>> , info_( text_buffer_.handle )
>> , text_buffer_activation_( text_buffer_.handle )
>> {
>> [&](){
>> Console_info params = info_.api_info();
>>
>> params.dwSize = {short( size.x ), short( size.y )};
>> params.dwCursorPosition = {};
>> //screen.wAttributes = bitsForBgFg(
>> params.srWindow = {0, 0, short( size.x - 1 ), short( size.y - 1 )};
>> params.dwMaximumWindowSize = params.dwSize;
>> info_.make_real( params );
>> }();
>> impl::winapi::set_output_mode(
>> text_buffer_.handle, impl::winapi::Output_flags::Enum()
>> );
>> }
>
> I don't get it. What does this that the following does not?
>
> Impl( const Point size )
> : text_buffer_()
> , info_( text_buffer_.handle )
> , text_buffer_activation_( text_buffer_.handle )
> {
> {
> Console_info params = info_.api_info();
>
> params.dwSize = {short( size.x ), short( size.y )};
> params.dwCursorPosition = {};
> //screen.wAttributes = bitsForBgFg(
> params.srWindow = {0, 0, short( size.x - 1 ), short( size.y - 1 )};
> params.dwMaximumWindowSize = params.dwSize;
> info_.make_real( params );
> }
> impl::winapi::set_output_mode(
> text_buffer_.handle, impl::winapi::Output_flags::Enum()
> );
> }

You're right, a simple curly braces block with appropriate comment is
equally clear.

It was a late night posting, sorry. I had planned to have the "params"
variable, the logical "with" variable, as a default-initialized formal
argument to the lambda. But the initializer expression, using a member
variable, can't be used as initializer for a formal argument because the
outer this can't be captured in the formal argument list.

So it was like triple dumb, sorry. But when I posted it it looked neat. :)


> Regarding lambdas and withs, I'd have thought more in the
> direction of
>
> #include <stdio.h>
> #include <functional>
>
> void with_open_file
> ( const char * const filename, const char * const mode,
> ::std::function<void(FILE *)> client )
> { FILE * f = fopen( filename, mode );
> if( f )client( f );
> fclose( f ); }
>
> int main()
> { with_open_file( "C:\\example\\test.txt", "w",
> []( FILE * f ){ fprintf( f, "test\n" ); }); }
>
> Of course, in C++ this style of withs todays is needed
> rarely because in C++ RAII is a better solution of the
> same problem. But this is what a with is to me.

Yes yes.


Cheers!, & thanks,

- Alf

Öö Tiib

unread,
May 7, 2016, 2:07:20 AM5/7/16
to
On Friday, 6 May 2016 04:32:58 UTC+3, Alf P. Steinbach wrote:
>
> Side note: what does one call something when Microsoft calls it "info"?
> In this case it's a set of values that specify the properties of a console.

I think of "info" as "answer to question of some kind". It can have more
accurate name to it (in context). If the info is carrying properties or
settings then saying that out in name may be better.

Regardless of the name when there is a "manager" of same thing (say
"console_screen_buffer_manager") then that is considered "Doer And Knower"
antipattern by many.

If it is not meant for language-neutral usage then lot of people
prefer scopes and qualification in C++ to long names (like
"console::screen_buffer::settings").

Alf P. Steinbach

unread,
May 7, 2016, 6:54:14 AM5/7/16
to
"settings" is good. Thank you.


Cheers!,

- Alf

Juha Nieminen

unread,
May 9, 2016, 2:28:55 AM5/9/16
to
Alf P. Steinbach <alf.p.stein...@gmail.com> wrote:
> Impl( const Point size )
> : text_buffer_()
> , info_( text_buffer_.handle )
> , text_buffer_activation_( text_buffer_.handle )
> {

That's some of the most confusing code I have seen in a long while.

I that even supposed to be standard C++? I suppose that if Impl() is
a precompiler macro hiding some extra code, you *could* make that
compile as C++, but...

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

Alf P. Steinbach

unread,
May 9, 2016, 10:21:22 AM5/9/16
to
On 09.05.2016 08:28, Juha Nieminen wrote:
> Alf P. Steinbach <alf.p.stein...@gmail.com> wrote:
>> Impl( const Point size )
>> : text_buffer_()
>> , info_( text_buffer_.handle )
>> , text_buffer_activation_( text_buffer_.handle )
>> {
>
> That's some of the most confusing code I have seen in a long while.
>
> I that even supposed to be standard C++? I suppose that if Impl() is
> a precompiler macro hiding some extra code, you *could* make that
> compile as C++, but...

It's called a “memory initializer list”.

Any good introductory book explains it.

If your book doesn't, then perhaps take a look at C++ book list at Stack
Overflow, <url:
http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list>.


Cheers & hth.,

- Alf

Alf P. Steinbach

unread,
May 9, 2016, 10:22:52 AM5/9/16
to
On 09.05.2016 16:20, Alf P. Steinbach wrote:
> On 09.05.2016 08:28, Juha Nieminen wrote:
>> Alf P. Steinbach <alf.p.stein...@gmail.com> wrote:
>>> Impl( const Point size )
>>> : text_buffer_()
>>> , info_( text_buffer_.handle )
>>> , text_buffer_activation_( text_buffer_.handle )
>>> {
>>
>> That's some of the most confusing code I have seen in a long while.
>>
>> I that even supposed to be standard C++? I suppose that if Impl() is
>> a precompiler macro hiding some extra code, you *could* make that
>> compile as C++, but...
>
> It's called a “memory initializer list”.

/member/ initializer list, sorry.

It's not the first time that that word substitution has happened to me. :(

It's probably like a melody that gets stuck in one's brain, difficult to
get rid of.

Öö Tiib

unread,
May 10, 2016, 5:10:07 AM5/10/16
to
On Monday, 9 May 2016 17:22:52 UTC+3, Alf P. Steinbach wrote:
> On 09.05.2016 16:20, Alf P. Steinbach wrote:
> > On 09.05.2016 08:28, Juha Nieminen wrote:
> >> Alf P. Steinbach <alf.p.stein...@gmail.com> wrote:
> >>> Impl( const Point size )
> >>> : text_buffer_()
> >>> , info_( text_buffer_.handle )
> >>> , text_buffer_activation_( text_buffer_.handle )
> >>> {
> >>
> >> That's some of the most confusing code I have seen in a long while.
> >>
> >> I that even supposed to be standard C++? I suppose that if Impl() is
> >> a precompiler macro hiding some extra code, you *could* make that
> >> compile as C++, but...
> >
> > It's called a "memory initializer list".
>
> /member/ initializer list, sorry.
>
> It's not the first time that that word substitution has happened to me. :(
>
> It's probably like a melody that gets stuck in one's brain, difficult to
> get rid of.

IMHO Juha Nieminen was complaining that the 'Impl' is confusing and likely
hiding vital things he expects to see. I do not think that he does not
know what member initializer list is.

Alf P. Steinbach

unread,
May 10, 2016, 9:03:16 AM5/10/16
to
Oh, then I should direct him elsewhere, even though a good intro book
should IMHO discuss the PIMPL idiom.

Herb Sutter (chair of the standardization committee, chief architect of
Visual C++) discusses it in a number of Guru Of The Week postings, see
<url: https://www.google.no/search?q=gotw+pimpl>

Incidentally, one of those postings is infamous (among the cognoscenti)
for abusing `std::auto_ptr` as a pointer to implementation. :)

PIMPL is discussed more in general in numerous articles all over the
web, including in Wikipedia, which chooses to call it Opaque Pointer; see

<url: https://www.google.no/search?q=pimpl+idiom>.

In short it's a well known and quite common idiom.

Including the naming.

Stuart Redmann

unread,
May 11, 2016, 2:01:54 AM5/11/16
to
On 10.05.2016, Öö Tiib wrote:
>> IMHO Juha Nieminen was complaining that the 'Impl' is confusing and likely
>> hiding vital things he expects to see. I do not think that he does not
>> know what member initializer list is.

Alf P. Steinbach wrote:
> Oh, then I should direct him elsewhere, even though a good intro book
> should IMHO discuss the PIMPL idiom.
[snip]

I'm pretty sure that Juha knows about The pimpl pattern as well.

Regards,
Stuart


Alf P. Steinbach

unread,
May 11, 2016, 7:49:19 PM5/11/16
to
That's a pretty obscure comment. What (the fuck) do you think he meant
then? We seem to have eliminated all possible sources of his confusion.

Cheers!,

- Alf



0 new messages