C ++: a way to declare a variable (or more than one) in an if statement that separates the variable definition and the test?

You can do the following:

case WM_COMMAND: if (WORD wNotifyCode = HIWORD(wparam)) { ... } 

And you can do it:

 case WM_COMMAND: { WORD wNotifyCode = HIWORD(wparam); if (wNotifyCode > 1) { ... } } 

But you can’t do it:

 case WM_COMMAND: if ((WORD wNotifyCode = HIWORD(wparam)) > 1) { ... } 

Using the for statement here is, I think, misleading:

 case WM_COMMAND: for (WORD wNotifyCode = HIWORD(wparam); wNotifyCode > 1; wNotifyCode = 0) { ... } 

Because it is very similar to a cycle, and the poor smack that follows me should decipher this garbage.

But is there a syntactic construct that combines the elegance of an if statement, including declaring a local variable, with the ability to check its value for something other than zero?

+4
source share
6 answers

Sometimes readability and maintainability are more important than a line with saved code.

IF you need a local variable in general, but in every sense I explicitly represent it in this case and maybe add an extra area if you want to limit it - but you should also think, maybe you can just live using the HIWORD macro in several places - so you don’t need any tricks at all.

+3
source

Preprocessor Tricks:

 #define IF_2(init, test) \ for (bool first_ = true; first_;) for (init; first_ && (test); first_ = false) IF_2(WORD wNotifyCode = HIWORD(wparam), wNotifyCode > 1) { ... } 

This is ugly and certainly not better than you already have.

+1
source

Try entering an auxiliary function as follows:

 template <typename T> T zeroIfLess(T val, T base) { return val < base ? T(0) : val; } 

Then write your condition as:

 if (WORD wNotifyCode = zeroIfLess(HIWORD(wparam), 2)) 

This returns zero β€” or, if you wish, false β€” if the first value supplied is less than the second; otherwise, it returns a value. Given that it is difficult to determine the meaning of a function name and whether to use the inclusive or exclusive minimum that it works here, this does not reduce it, as a strange hack.

Like others, I also approve of your first sentence after "And You Can Do It" - a separate expression and an initialization expression, followed by a conditional statement. I think this is a natural way to do this in C ++.

+1
source

Tip: you can use cracker message macros; this way you will get a much shorter wndproc (without all of these nested switches), your message processing code will be neatly divided into separate functions (one for each message), and you will hardly need all HIWORD-LOWORD, because message macros are crackers do this for you and pass the information received by lParam and wParam to you already divided function in the parameters.

+1
source

You can change your test a little:

 if (WORD wNotifyCode = HIWORD(wparam) - 1) 

if you want to check if wNotifyCode > 1 .

-2
source

Following work for me

 if (WORD nNotifyCode = HIWORD(test) > 1) { } 

I would venture to suggest, but I don’t know for sure that the = operator takes precedence over the> operator, and I know that the result of the assignment operation is the destination value, the test works.

EDIT: [Room Dunce Cap, transition to the corner]

-2
source

Source: https://habr.com/ru/post/1300460/


All Articles