Is it possible to conditionally choose which variable to assign?

Essentially the same as this question , but for C ++.

The easiest way to do this:

if (condition) { a = f(x); } else { b = f(x); } 

While this meets my needs, I always had to worry about having to repeat the typing of f (x) twice, not to mention the extension of the code due to the large number of lines when only one line will be executed. What if I have a larger set of destination variables to choose from?

 switch(condition variable) { case 1: var1 = f(x); break; case 2: var2 = f(x); break; ... case y: vary = f(x); break; } 

It looks very illogical to me.

Is there anything in C ++ that essentially allows me to do the following?

 do-something-that-returns-a-lvalue = f(x); 

Forgive me if this is not called lvalue, I'm still relatively new to C ++. But you guys know what I mean - an assignment variable to assign a value.

+5
source share
4 answers

You can form a link or a pointer to the variable that you want to assign.

 (condition ? a : b) = f(x); // reference 

The above version was suggested by aschepler in the comments.

 *(condition ? &a : &b) = f(x); // pointer 

The reference version is basically equivalent to the following more detailed example using a helper function.

 template <typename T> T& conditional_reference(bool pred, T& yes, T& no) noexcept { return pred ? yes : no; } // And then later conditional_reference(condition, a, b) = f(x); 

I want to say that your traditional if then else thread is probably still the best choice in terms of code clarity.

If the assignment source is a more complex expression than just f(x) , you can save its result in a temporary variable, and then assign this variable only in the conditional expression. The semantics of C ++ 11 relocation in many cases allows the use of such temporary files.

+8
source

If you can decide which variable to assign at compile time, you can get away with this very clean and null overhead:

 std::get<constexpr_indexer()>(std::tie(var1, var2, var3, var4)) = f(x); 

std::get , std::tie p>

+6
source

You can assign a pointer variable (for example, double * p ) in a switch statement.

 switch(condition variable) { case 1: p = & var1; break; case 2: p = & var2; break; ... case y: p = & vary; break; } 

You can then do this after switching the operator.

 *p = f(x); 
+2
source

Like the question you contacted, you still have an XY problem. Why do you need it? Why do you feel you are writing more code that is potentially unreadable and not subject to elegance than the simple if / else condition? But the gist of the question is, in which correct scenario would you have 26 (following the alphabet) variables with the same value?

If you have a script similar to the original question (binary search tree), just stick to one variable.

Read DRY, irrelevant, but almost identical code :

You do not do DRY because someone wrote it in a book somewhere that it’s good to do it, you do DRY because it really has tangible advantages.

0
source

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


All Articles