How to have three 'logical' states in C ++

What is the best way to have three values ​​of a boolean variable in C ++?

I would like the fields to be set to true , false or not to be set at all in my array.

If I declare them as follows:

 t[0] = true; t[1] = false; t[2] = NULL; 

When I check the condition that I get:

t[2] false

+7
source share
6 answers

This should work:

 t[0] = true; t[1] = false; t[2] = -1; 

Or, if you only need 3 states, but maybe at some point you need more, enum fine:

 enum STATES { NULL_STATE = -1, // you can manually specify -1 to give it a special case value FALSE, // will be equal to 0 TRUE // will be equal to 1 }; 

Despite everything, 0/false is the only thing that returns false in the if() . -1 and true both return true .

You might want to use this switch to work with 3+ states:

 switch (var) // may need to cast: (int)var { case 1: case 0: case -1: }; 

Alternatively, if you want to stick with the if statement block, you can do something like this:

 if (var == -1) // or (var == NULL_STATE) {} else if (var) // true condition {} else // false {} 
+5
source
+3
source

Consider using std::experimental::optional<bool> (if you have a standard C ++ library) or boost::optional<bool> (www.boost.org).

I believe std::optional is a candidate for C ++ 17, so if you accept one of the above, your refactoring efforts in C ++ 17 should be minimal.

If you don’t like using things that are not (yet?) In the “correct” C ++ standard library, consider

  • Something based on std::unique_ptr<bool>

  • A std::pair<bool, bool>

  • Good old fashioned enum with 3 meanings.

+3
source

You can use boost :: optional

http://www.boost.org/doc/libs/1_60_0/libs/optional/doc/html/index.html

 boost::optional<bool> myBooleanVariable; 

I agree that tribool might be better if you don't need uninitialized values ​​like NULL. When comparing options and tribula, the documentation says:

Firstly, it is functionally similar to a tribate logical (false, possibly true), such as boost :: tribool, except that in tristate boolean, perhaps the state represents a real value, unlike the corresponding uninitialized state, optional. It should be carefully considered if an additional, rather than a tribune is really needed.

Source: http://www.boost.org/doc/libs/1_60_0/libs/optional/doc/html/boost_optional/a_note_about_optional_bool_.html

+2
source

What is the best way to have a boolean variable with three values ​​in c ++?

Boolean values, by definition, have only 2 possible states - True or False. If you want to have a different state for "incorrect" or "not set", then you need to encapsulate the bool variable in some other data types.

The correct solution depends on what you want to do with this variable. For simple comparisons (if-else and switch), preferred enumerations (c ++ 11) should be preferred.

 enum class tristate{true,false,undefined=0}; 

They are simple, easy to use and understand, and provide type safety compared to old enumerations. Since they are type-safe, you cannot accidentally compare them with different types of enumerations or number types, but it also means that you also cannot use bit tricks and integer tricks. If no other type is specified, the enumerated class is a numeric type that initializes to "0". this means that by assigning the value '0' to one of the enumerated values, you can make it the default state.

 tristatet[7]; t[1] = tristate::true; t[2] = tristate::false; t[3] = tristate::undefined; t[4] = false; //error t[5] = 0; //error t[6] = null; //error t[0] == true; //error t[0] == tristate::true; // false t[0] == tristate::undefined; // true 

Of course, you can use this in a switch statement:

 switch(t[2]) { case tristate::true: foo(); break; case tristate::false: bar(); break; //t[2] was set to tristate::false case tristate::undefined : doNothing(); break; } 
+1
source

You can use std::optional for this:

 std::optional<bool> t[3]; t[0] = true; t[1] = false; t[2] = std::nullopt; for (auto const& i : t) if (i.has_value()) std::cout << i.value() << '\n'; 

output:

 1 0 
0
source

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


All Articles