tags:

views:

653

answers:

6

tribool strikes me as one of the oddest corners of Boost. I see how it has some conveniences compared to using an enum but an enum can also be easily expanded represent more than 3 states.

In what real world ways have you put tribool to use?

+3  A: 

There are several real world examples on Google Code Search here.

Brian R. Bondy
+5  A: 

While I havn't used C++, and hence boost, I have used three-state variables quite extensively in a network application where I need to store state as true/false/pending.

Matthew Scharley
Yes, general async or polling situations seem to be a natural fit for tribool.
Duck
+6  A: 

An extra state in any value type can be extremely valuable. It avoids the use of "magic numbers" or extra flags to determine if the value of a variable is "maybe" or "unknown".

Instead of true or false, the state of a tribool is true, false, or indeterminate.

Let's say you have a database that contains a list of customers and their dateOfBirth. So you write a function along the lines of :

tribool IsCustomerAdult(customerName);

The function returns:

`true` if the customer is 18 or older;
`false` if the customer is less than 18;
`indeterminate` if the customer is not in the database 
     (or the dateOfBirth value is not present).

Very useful.

Robert Cartaino
I'd throw an exception if the customer wasn't found, but I can see how tri-state is useful in the latter.
GMan
for this scenario, `boost::optional` is an option too.
rafak
+2  A: 

I am a big fan of the Boost library and started using it at company who I have since left. After getting exposure to and using the boost library extensively throughout our project I stumbled on tribool and was considering using for some "Fuzzy Logic" algorithms needing improvements.

I left before I had a chance to get into it, but beyond the "Fuzzy Logic" example, other modules in the system had components with this sort of between state that considering now, I would probably end up using tribool in a decent amount of code if I was still with the company.

-bn

bn
+2  A: 

I've seen numerous examples of two booleans being used to represent three possible states, explicitly or otherwise, with the fourth state being silently assumed to be impossible. In at least two cases, I've changed such constructions to use tribool since we started using boost.

Tim Sylvester
Me too though the use of bools might have been inappropriate to begin with and since the app was mature I was confident that conditions (specs, etc) were not going to change to 4 or 5 states. Good use case.
Duck
+1  A: 

I think it is very useful for Language moulding such as OCR applications and Speech synthesis because as you know human languages are ambiguous and they have a lot of Intermediate statuses

looking foreword to improve the current technologies using the tribool

Hashem