Page 1 of 1

This is actually nice

Posted: April 29th, 2020, 6:55 pm
by albinopapa

Code: Select all

                pushEvent( {
                    Event::KeyPressed,
                    Event::KeyEvent{
                        .code = virtualKeyCodeToSF( wParam, lParam ),
                        .alt = HIWORD( GetKeyState( VK_MENU ) ) != 0,
                        .control = HIWORD( GetKeyState( VK_CONTROL ) ) != 0,
                        .shift = HIWORD( GetKeyState( VK_SHIFT ) ) != 0,
                        .system = HIWORD( GetKeyState( VK_LWIN ) ) || HIWORD( GetKeyState( VK_RWIN ) )
                    } }
                );
This C++20 feature is nice.

This would be equivalent to

Code: Select all

                pushEvent( {
                    Event::KeyPressed,
                    Event::KeyEvent{
                        virtualKeyCodeToSF( wParam, lParam ),
                        HIWORD( GetKeyState( VK_MENU ) ) != 0,
                        HIWORD( GetKeyState( VK_CONTROL ) ) != 0,
                        HIWORD( GetKeyState( VK_SHIFT ) ) != 0,
                        HIWORD( GetKeyState( VK_LWIN ) ) || HIWORD( GetKeyState( VK_RWIN ) )
                    } }
                );


But reads like and is as safe as:

Code: Select all

                Event::KeyEvent kevent;
                kevent.code = virtualKeyCodeToSF( wParam, lParam );
                kevent.alt = HIWORD( GetKeyState( VK_MENU ) ) != 0
                kevent.control = HIWORD( GetKeyState( VK_CONTROL ) ) != 0;
                kevent.shift = HIWORD( GetKeyState( VK_SHIFT ) ) != 0;
                kevent.system = HIWORD( GetKeyState( VK_LWIN ) ) || HIWORD( GetKeyState( VK_RWIN ) );

                pushEvent( { Event::KeyPressed, kevent } );
I say safer because .alt, .control, .shift and .system are all bool values so getting the order of initialization wrong would cause issues. With being able to specify the members in the braced initialization you get the safety of directly initializing the correct members and the efficiency of aggregate initialization.

I may have to give up on VS2017 now that more C++20 features are making it's way into VS2019.

Re: This is actually nice

Posted: May 2nd, 2020, 8:42 am
by chili
Oh, i didn't know that that one was available now. I should start using it.

Re: This is actually nice

Posted: May 3rd, 2020, 3:19 am
by albinopapa
I found it when I was looking through the roadmap link you had posted some where...here, discord or twitter, can't remember.

Re: This is actually nice

Posted: May 3rd, 2020, 3:21 am
by albinopapa
I keep forgetting about this syntax when needing a counter variable with an STL algorithm:

Code: Select all

auto create_thread = [ &, count = 0 ]( std::thread& t ) mutable
{ 
	t = std::thread{ task, count++ }; 
};
Never mind what I'm doing creating threads like this, the thing I'm pointing out is declaring the count inside the capture section and declaring the lambda as mutable so that count can be incremented.

Re: This is actually nice

Posted: May 11th, 2020, 2:13 pm
by cyboryxmen
I think you're actually nice Albinopapa

Re: This is actually nice

Posted: May 12th, 2020, 1:08 am
by albinopapa
Well, that came outta nowhere, I'm a little confused and flattered, thanks.