title: Primitive type wrapper in C++ that is both POD and constructible in an expression?
body: <p>I'd like to create a wrapper class for a primitive type in C++ so that I can overload functions and operators for it. However, I would otherwise like it to act exactly like the primitive type. For instance, I would like it to be a POD type, so it can be used in structures accessed by (separately compiled) C code, and be implicitly convertible to/from the primitive type. As a concrete example, I'd like something like <a href="http://www.boost.org/doc/libs/1_55_0/doc/html/tribool.html" rel="nofollow">Boost tribool</a>, but that is a POD with guaranteed <code>sizeof</code> of 1. C++03 compatibility is very desirable, but C++11-only might be useful too.</p>
  
  <p>It's pretty easy to get started with something like this:</p>
  
  <pre><code>struct tribool_pod {
      unsigned char value; // can be private in C++11
      tribool_pod&amp; operator=(unsigned char v) { value = v; return *this; }
      constexpr operator unsigned char() const { return value; }
      tribool_pod&amp; operator=(bool v) { value = v ? 1 : 0; return *this; }
      constexpr operator bool() const { return value == 1; }
  };
  </code></pre>
  
  <p>Note that there are no user-defined constructors, so that so far it is a POD type. However, I hit the first snag when writing the operators:</p>
  
  <pre><code>constexpr tribool_pod operator!() const {
      return value == 0 ? &lt;true-value&gt; :
             value == 1 ? &lt;false-value&gt; :
             &lt;indeterminate-value&gt;;
  }
  </code></pre>
  
  <p>In this case, what is the best way to create an instance in an expression for things like <code>&lt;true-value&gt;</code>? In C++11, I can use <code>tribool_pod{true}</code>, but is there any way in C++03? If structs in C++03 can only be initialized in a variable declaration, the only way I can think to write constant expressions for things like <code>&lt;true-value&gt;</code> is to refer to a <code>static const tribool_pod</code> (which has external linkage). That has the drawback of not being header-only and is likely less efficient than using the primitive type.</p>
  
  <p>A related problem even in C++11 is that I don't have a conversion from <code>bool</code> to <code>tribool_pod</code>. Obviously, if I add a conversion constructor to <code>tribool_pod</code>, it is no longer a POD.</p>
  
  <p>Is there some trick I'm missing, or is this fundamentally impossible? This seems like an unfortunate consequence of C++ constructors being an all or nothing proposition: Constructors are the only implicit conversion function, but if you define any, one is always called, and you're no longer a POD type. (Or in C++11 terms, it has standard layout, but is not trivial(ly constructible)).</p>
  
  <p>Or maybe I'm worrying too much about POD-ness? If I include conversion constructors and a no-op default constructor, what do I lose? Just super-obscure things like <code>goto</code> over initialization? I figure I can probably assume standard layout (e.g. no padding at the beginning of the object) with C++03 from real-world compilers, even though the spec doesn't guarantee it.</p>
  
  <p><strong>Note</strong>: Part of the reason I care about this is because I want the same headers usable from C (with degraded functionality and safety). For example:</p>
  
  <pre><code>#ifdef __cplusplus
  struct tribool_pod { unsigned char value; ... };
  #else
  typedef unsigned char tribool_pod;
  #endif
  </code></pre>