Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 611453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:47:08+00:00 2026-05-13T17:47:08+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:47:09+00:00Added an answer on May 13, 2026 at 5:47 pm
    bool?
    

    done. That do? In particular, the C# compiler will provide the “lifted” operators to map to bool for you. Arguably, though, it might be slightly larger than just a bool or 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 an enum (possibly a byte enum, but I’d default to int as normal):

    public struct Tribool : IEquatable<Tribool> {
        public static Tribool True { get { return new Tribool(true); } }
        public static Tribool False { get { return new Tribool(false); } }
        public static Tribool Unknown { get { return new Tribool(); } }
        enum TriboolState { Unknown = 0, True = 1, False = 2 }
    
        private readonly TriboolState state;
        public Tribool(bool state) {
            this.state = state ? TriboolState.True : TriboolState.False;
        }
        // default struct ctor handles unknown
        public static bool operator true(Tribool value) {
            return value.state == TriboolState.True;
        }
        public static bool operator false(Tribool value) {
            return value.state == TriboolState.False;
        }
        public static bool operator ==(Tribool x, Tribool y) {
            return x.state == y.state;
        }
        public static bool operator !=(Tribool x, Tribool y) {
            return x.state != y.state; // note: which "unknown" logic do you want?
                                       // i.e. does unknown == unknown?
        }
        public override string ToString() {
            return state.ToString();
        }
        public override bool Equals(object obj) {
            return (obj != null && obj is Tribool) ? Equals((Tribool)obj) : false;
        }
        public bool Equals(Tribool value) {
            return value == this;
        }
        public override int GetHashCode() {
            return state.GetHashCode();
        }
        public static implicit operator Tribool(bool value) {
            return new Tribool(value);
        }
        public static explicit operator bool(Tribool value) {
            switch (value.state) {
                case TriboolState.True: return true;
                case TriboolState.False: return false;
                default: throw new InvalidCastException();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 373k
  • Answers 373k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Disclaimer this is off the top of my head, I… May 14, 2026 at 7:37 pm
  • Editorial Team
    Editorial Team added an answer Actually there could be valid reasons to create a non-clustered… May 14, 2026 at 7:37 pm
  • Editorial Team
    Editorial Team added an answer NSDictionary. You can't simply say NSMutableDictionary *_myDict = [contentArray objectAtIndex:0];… May 14, 2026 at 7:37 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.