How to implement flags with the options true,false,default and toggle in C++?
<p>I'm currently trying to come up with a clever way of implementing flags that include the states "default" and (optional) "toggle" in addition to the usual "true" and "false".</p>

<p>The general problem with flags is, that one has a function and wants to define its behaviour (either "do something" or "don't do something") by passing certain parameters.</p>

<h2>Single flag</h2>

<p>With a single (boolean) flag the solution is simple:</p>

<pre><code>void foo(...,bool flag){
    if(flag){/*do something*/}
}
</code></pre>

<p>Here it is especially easy to add a default, by just changing the function to</p>

<pre><code>void foo(...,bool flag=true)
</code></pre>

<p>and call it without the flag parameter.</p>

<h2>Multiple flags</h2>

<p>Once the number of flags increases, the solution i usually see and use is something like this:</p>

<pre><code>typedef int Flag;
static const Flag Flag1 = 1&lt;&lt;0;
static const Flag Flag2 = 1&lt;&lt;1;
static const Flag Flag3 = 1&lt;&lt;2;

void foo(/*other arguments ,*/ Flag f){
    if(f &amp; Flag1){/*do whatever Flag1 indicates*/}
    /*check other flags*/
}

//call like this:
foo(/*args ,*/ Flag1 | Flag3)
</code></pre>

<p>This has the advantage, that you don't need a parameter for each flag, which means the user can set the flags he likes and just forget about the ones he don't care about. Especially you dont get a call like <code>foo (/*args*/, true, false, true)</code> where you have to count which true/false denotes which flag.</p>

<p>The problem here is:<br>
If you set a default argument, it is overwritten as soon as the user specifies any flag. It is not possible to do somethink like <code>Flag1=true, Flag2=false, Flag3=default</code>.</p>

<p>Obviously, if we want to have 3 options (true, false, default) we need to pass at least 2 bits per flag. So while it might not be neccessary, i guess it should be easy for any implementation to use the 4th state to indicate a toggle (= !default).</p>

<p>I have 2 approaches to this, but i'm not really happy with both of them:</p>

<h2>Approach 1: Defining 2 Flags</h2>

<p>I tried using something like this up to now:</p>

<pre><code>typedef int Flag;
static const Flag Flag1 = 1&lt;&lt;0;
static const Flag Flag1False= 1&lt;&lt;1;
static const Flag Flag1Toggle = Flag1 | Flag1False;
static const Flag Flag2= 1&lt;&lt;2;
static const Flag Flag2False= 1&lt;&lt;3;
static const Flag Flag2Toggle = Flag2 | Flag2False;

void applyDefault(Flag&amp; f){
    //do nothing for flags with default false

    //for flags with default true:
    f = ( f &amp; Flag1False)? f &amp; ~Flag1 : f | Flag1;
    //if the false bit is set, it is either false or toggle, anyway: clear the bit
    //if its not set, its either true or default, anyway: set
}

void foo(/*args ,*/ Flag f){
    applyDefault(f);

    if (f &amp; Flag1) //do whatever Flag1 indicates
}
</code></pre>

<p>However what i don't like about this is, that there are two different bits used for each flag. This leads to the different code for "default-true" and "default-false" flags and to the neccessary if instead of some nice bitwise operation in <code>applyDefault()</code>.</p>

<h2>Approach 2: Templates</h2>

<p>By defining a template-class like this:</p>

<pre><code>struct Flag{
  virtual bool apply(bool prev) const =0;
};

template&lt;bool mTrue, bool mFalse&gt;
struct TFlag: public Flag{
    inline bool apply(bool prev) const{
        return (!prev&amp;&amp;mTrue)||(prev&amp;&amp;!mFalse);
    }
};

TFlag&lt;true,false&gt; fTrue;
TFlag&lt;false,true&gt; fFalse;
TFlag&lt;false,false&gt; fDefault;
TFlag&lt;true,true&gt; fToggle;
</code></pre>

<p>i was able to condense the <code>apply</code> into a single bitwise operation, with all but 1 argument known at compile time. So using the <code>TFlag::apply</code> directly compiles (using gcc) to the same machine code as a <code>return true;</code>, <code>return false;</code>, <code>return prev;</code> or <code>return !prev;</code> would, which is pretty efficient, but that would mean i have to use template-functions if i want to pass a <code>TFlag</code> as argument. Inheriting from <code>Flag</code> and using a <code>const Flag&amp;</code> as argument adds the overhead of a virtual function call, but saves me from using templates.</p>

<p>However i have no idea how to scale this up to multiple flags...</p>

<h2>Question</h2>

<p>So the question is:
How can i implement multiple flags in a single argument in C++, so that a user can easily set them to "true", "false" or "default" (by not setting the specific flag) or (optional) indicate "whatever is not default"?</p>

<p>Is a class with two ints, using a similar bitwise operation like the template-approach with its own bitwise-operators the way to go? And if so, is there a way to give the compiler the option to do most of the bitwise operations at compile-time?</p>

<p><strong>Edit for clarification:</strong>
I don't want to pass the 4 distinct flags "true", "false", "default", "toggle" to a function.<br>
E.g. think of a circle that gets drawn where the flags are used for "draw border", "draw center", "draw fill color", "blurry border", "let the circle hop up and down", "do whatever other fancy stuff you can do with a circle", ....<br>
And for each of those "properties" i want to pass a flag with value either true, false, default or toggle.
So the function might decide to draw the border, fill color and center by default, but none of the rest. A call, roughly like this:</p>

<pre><code>draw_circle (DRAW_BORDER | DONT_DRAW_CENTER | TOGGLE_BLURRY_BORDER) //or
draw_circle (BORDER=true, CENTER=false, BLURRY=toggle)
//or whatever nice syntax you come up with....
</code></pre>

<p>should draw the border (specified by flag), not draw the center (specified by flag), blurry the border (the flag says: not the default) and draw the fill color (not specified, but its default).<br>
If i later decide to not draw the center by default anymore but blurry the border by default, the call should draw the border (specified by flag), not draw the center (specified by flag), <em>not</em> blurry the border (now blurrying is default, but we don't want default) and draw the fill color (no flag for it, but its default).</p>