I’m trying to implement a Tribool type using http://www.boost.org/doc/libs/1_41_0/doc/html/tribool.html as reference.
I’m using a struct since it’s a primitive type and shouldn’t be extendable. I know there are 3 types of Tribools—True, False, and Unknown, and the default constructor should provide a False Tribool. My question is, what data type do I set True, False and Unknown to? Right now I have:
struct Tribool
{
//True, False, and Unknown public constants
public static readonly Tribool TRUE, FALSE, UNKNOWN;
//Constructors
public Tribool()
{
Tribool = FALSE; //use ValueType instead?
}
but I’m not sure if that’s correct, since it looks like I’m just setting a Tribool to another Tribool. Should I use ValueType instead? It popped up when I was typing in VS, and it sounds sensible, but I wasn’t able to find a lot of information on it from Google.
Also, the Tribool needs to be able to operate with regular bools, which means “true” and “false” need to be overloaded. Would this require an operator overload? Or should it just be a method that returns a bool?
Thanks in advance!
Edit: Sorry, I should have mentioned this was for an assignment. So I can’t use bools even though it’s a lot more practical as many of you have pointed out.
done. That do? In particular, the C# compiler will provide the “lifted” operators to map to
boolfor you. Arguably, though, it might be slightly larger than just aboolor single enum.Note: don’t use
ValueType, as this would actually be a boxing operation.If you couldn’t use
bool?(i.e. you wanted to implement from scratch) I would map it to anenum(possibly abyteenum, but I’d default tointas normal):