tribool让我成为Boost最奇怪的角落之一.与使用枚举相比,我看到它有一些方便性,但枚举也可以轻松扩展,代表3种以上的状态.
你用现实世界的方式使用tribool吗?
Mat*_*ley 19
虽然我没有使用C++,因此我已经在网络应用程序中广泛使用了三态变量,我需要将状态存储为true/false/pending.
Rob*_*ino 15
任何值类型的额外状态都非常有价值.它避免使用"幻数"或额外标志来确定变量的值是"可能"还是"未知".
取而代之的true或false,的状态tribool是true,false或indeterminate.
假设您有一个包含列表customers及其列表的数据库dateOfBirth.所以你写了一个函数:
tribool IsCustomerAdult(customerName);
Run Code Online (Sandbox Code Playgroud)
该函数返回:
Run Code Online (Sandbox Code Playgroud)`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).
很有用.
小智 9
我认为额外的好处不仅是第3个值,而且还可以轻松使用3值逻辑!
例如:
(true && indeterminate) == indeterminate
(true || indeterminate) == true
Run Code Online (Sandbox Code Playgroud)
SQL实现了这样的逻辑.
我已经看到了很多例子,两个布尔值被用来代表三个可能的状态,明确地或其他地方,第四个状态被默默地认为是不可能的.至少在两种情况下,我已经改变了这样的结构,因为我们开始使用boost来使用tribool.