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