title: How can I create a new primitive type using C++11 style strong typedefs?
body: <p>I'm trying to emulate in C++ a <a href="http://nim-lang.org/docs/manual.html#types-distinct-type" rel="nofollow noreferrer">distinct
  type</a> from the <a href="http://nim-lang.org" rel="nofollow noreferrer">Nim
  programming language</a>. The following example won't
  compile in Nim because the compiler catches the variables <code>e</code> and <code>d</code>
  having different types (<code>Error: type mismatch: got (Euros, float)</code>) despite
  both being a float at the binary level:</p>
  
  <pre><code>type
    Euros = distinct float
  
  when isMainModule:
    var
      e = Euros(12.34)
      d = 23.3
    echo (e + d)
  </code></pre>
  
  <p>One way to do this in C++ would be to write a wrapper class for floats. <strike>But
  this doesn't work well with APIs which export the type because the size won't
  be the same as float. Or even if a class' size matches the storage length of a
  float, it will never match the size of a char type.</strike> That will work if you also implement all possible operators for operations like addition, substraction, etc, but requires much typing and duplication of code.</p>
  
  <p>Older questions like <a href="https://stackoverflow.com/questions/1572420/creating-a-new-primitive-type">Creating a new primitive
  type</a>
  have as accepted answer to use boost's strong typedef. However the typedef
  seems to work only for function type signatures, the typedef won't prevent two
  float-inherited types to be added together and their type completely changed
  (well, because there is just the <em>illusion</em> of a new type):</p>
  
  <pre><code>#include &lt;boost/serialization/strong_typedef.hpp&gt;
  #include &lt;stdio.h&gt;
  
  BOOST_STRONG_TYPEDEF(float, money);
  
  void test(money a, float b)
  {
      int t = a + b;
      printf("value is %d", t);
  }
  
  int main()
  {
      money a(5.5);
      int euros(5);
      // This is not caught!
      int dollars = a + euros;
      printf("dollars %d\n", dollars);
      // But the compiler catches this misuse.
      test(euros, a);
  }
  </code></pre>
  
  <p>But that's nearly it, the <code>test()</code> call won't work because the signature
  doesn't match, but the language still allows other operations to mangle types
  at will.</p>
  
  <p>That same answer mentions C++0x to bring strong typedefs, so I looked for this
  new support and found that <a href="http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Keynote-Bjarne-Stroustrup-Cpp11-Style" rel="nofollow noreferrer">Bjarne Stroustrup himself gave a C++11 style
  keynote in
  2012</a>.
  Around minute 21 he starts talking about these new strong typedefs. If you
  download just the slides, page 19 starts talking about <em>SI Units</em> and later
  page 22 and 23 mention how this would be done. However, I have been unable to
  make the examples work. Here's the patchwork I managed to concoct:</p>
  
  <pre><code>template&lt;int M, int K, int S&gt; struct Unit { // a unit in the MKS system
      enum { m=M, kg=K, s=S };
  };
  template&lt;typename Unit&gt; // a magnitude with a unit
  struct Value {
      double val; // the magnitude
      explicit Value(double d) : val(d) {} // construct a Value from a double
  };
  
  using Meter = Unit&lt;1,0,0&gt;; // unit: meter
  using Second = Unit&lt;0,0,1&gt;; // unit: sec
  using Speed = Value&lt; Unit&lt;1,0,-1&gt; &gt;; // meters/second type
  constexpr Value&lt;Second&gt; operator "" _s(long double d)
  // a f-p literal suffixed by ‘_s’
  {
  return Value&lt;Second&gt; (d);
  }
  constexpr Value&lt;Meter&gt; operator "" _m(long double d)
  // a f-p literal suffixed by ‘_m’
  {
  return Value&lt;Meter&gt; (d);
  }
  
  int main(void)
  {
      Speed sp1 = 100_m / 9.8_s;
      return 42;
  }
  </code></pre>
  
  <p>I'm trying to compile this under MacOSX with the latest Xcode 5.1.1 with the command line:</p>
  
  <pre><code>$ g++ unit.cpp -std=c++11
  unit.cpp:13:25: error: constexpr function's return type 'Value&lt;Second&gt;' is not a
        literal type
  constexpr Value&lt;Second&gt; operator "" _s(long double d)
                          ^
  unit.cpp:5:8: note: 'Value&lt;Unit&lt;0, 0, 1&gt; &gt;' is not literal because it is not an
        aggregate and has no constexpr constructors other than copy or move
        constructors
  struct Value {
         ^
  unit.cpp:18:24: error: constexpr function's return type 'Value&lt;Meter&gt;' is not a
        literal type
  constexpr Value&lt;Meter&gt; operator "" _m(long double d)
                         ^
  unit.cpp:5:8: note: 'Value&lt;Unit&lt;1, 0, 0&gt; &gt;' is not literal because it is not an
        aggregate and has no constexpr constructors other than copy or move
        constructors
  struct Value {
         ^
  unit.cpp:26:20: error: no matching literal operator for call to 'operator "" _m'
        with argument of type 'unsigned long long' or 'const char *', and no
        matching literal operator template
      Speed sp1 = 100_m / 9.8_s;
                     ^
  unit.cpp:26:28: error: no matching literal operator for call to 'operator "" _s'
        with argument of type 'long double' or 'const char *', and no matching
        literal operator template
      Speed sp1 = 100_m / 9.8_s;
                             ^
  4 errors generated.
  </code></pre>
  
  <p>Maybe the examples given in the slides and I'm missing some more code? Does somebody have a complete example of what Bjarne was trying to demonstrate?</p>