This article was provoked by an interesting topic on
habrahabr.ru/post/166201 about the Switch Operator for C ++ and the desire to provide the community with another idea and embodiment for evaluation.
One of the drawbacks of the Switch operator in C ++ is the limitation on the type of data to which it can be used and the author of the article has provided an interesting solution by reference.
I will try to propose another solution, perhaps not so good, but not requiring additional language features and working for any type of variables in C ++ and also processing all possible truth options for the conditions given.
')
Those. Another “switch for strings” - but not only for strings and not just “switch”.
The given solution does not claim originality and it is quite expected that the idea has already appeared on the web pages more than once, but in the form in which I have not yet seen it, I apologize if this option has already been described by someone.
The idea is to use the ternary operator?: In the Switch condition
let's say “test” test variable and “var_ i” i = 1, .., 4 are variables with which to compare it
from the design
bool bool_1, bool_2, bool_3, bool_4;
bool_1 = (test == var_1);
bool_2 = (test == var_2);
bool_3 = (test == var_3);
bool_4 = (test == var_4);
it can be seen that bool_ ii = 1, .., 4 are logical conditions and the variables test and var_i i = 1, .., 4 can be of any type, most importantly they are of the same type.
idea in the next line
switch ((bool_1? 1: 0) | (bool_2? 2: 0) | ((bool_3? 4: 0) | (bool_4? 8: 0))
and is that the switch will receive for deciding the result of the operation “or” on binary powers of twos.
This gives not only the classic switch, but also the promised processing of all truthful variants, i.e. all 16 options for 4 conditions.
Conditions in switch can be added in any quantity while maintaining successive powers of two in the second argument of each ternary operator
The explanation further:
First, about the ternary operator - he, as is known, will choose the power of two or the next 0, according to the truth of the variable bool_ ii = 1, .., 4. The powers of two are chosen, of course, not by chance, they provide an opportunity to process all possible truthful variants.
If we have one and only one of the conditions of bool_ i, then the switch will work in the classic way and choose bool_ i since all the others will give 0. That is, The result of the switch is the condition in which the variable bool_ i is true.
If several different bool_ i variables are true, then in the switch condition a mathematical disjunction of several different powers of two will appear. Since these are different binary powers of twos, they have units at different places and will remain as a result of the disjunction while the remaining places will have 0
Those. Switch will give a switch to the case where just these several different bool_ i variables are true and the other conditions are false.
Below is a working version in which in the classical case of truth of only one of the conditions bool_ i, the test variable test is assigned the corresponding value of the var_i variable.
For the remaining variants of the truth distribution, I gave the corresponding descriptions of truth in English.
switch ((bool_1? 1: 0) | (bool_2? 2: 0) | ((bool_3? 4: 0) | (bool_4? 8: 0))
{
case 1: test = var_1;
break;
case 2: test = var_2;
break;
case 4: test = var_3;
break;
case 8: test = var_4;
break;
case 3: std :: cout << «bool_1 bool_2 true bool_3 bool_4 false» << std :: endl;
break;
case 5: std :: cout << «bool_1 bool_3 true bool_2 bool_4 false» << std :: endl;
break;
case 9: std :: cout << «bool_1 bool_4 true bool_2 bool_3 false» << std :: endl;
break;
case 6: std :: cout << «bool_2 bool_3 true bool_1 bool_4 false» << std :: endl;
break;
case 10: std :: cout << «bool_2 bool_4 true bool_1 bool_3 false» << std :: endl;
break;
case 12: std :: cout << «bool_3 bool_4 true bool_1 bool_2 false» << std :: endl;
break;
case 7: std :: cout << «bool_3 bool_2 bool_1 true bool_4 false» << std :: endl;
break;
case 11: std :: cout << «bool_4 bool_2 bool_1 true bool_3 false» << std :: endl;
break;
case 14: std :: cout << «bool_4 bool_2 bool_3 true bool_1 false» << std :: endl;
break;
case 13: std :: cout << «bool_4 bool_1 bool_3 true bool_2 false» << std :: endl;
break;
case 15: std :: cout << «bool_4 bool_1 bool_3 bool_2 true» << std :: endl;
break;
default: std :: cout << «this is the same as 0 bool_1 bool_2 bool_3 bool_4 false» << std :: endl;
}
The given variant is checked under debian.
Of course, you can write a script that automatically generates such a switch not only for 4 but also for any number of conditions. Since switch has no limit on the number of conditions, only the memory used will be the limit.
Thank you in advance for all comments and considerations.