title: How best to implement the "newtype" idiom in C++?
body: <p>Since learning Rust, I've become a fan of the <a href="https://doc.rust-lang.org/stable/rust-by-example/generics/new_types.html" rel="noreferrer">newtype idiom</a> which I gather Rust borrowed <a href="https://wiki.haskell.org/Newtype" rel="noreferrer">from Haskell</a>.</p>
  <p>A newtype is a distinct type based on a standard type which ensures that function parameters are of the correct type.<br />
  For example, the <code>old_enough</code> function below must be passed an age in Years. It will not compile with an age in Days or as a plain i64.</p>
  <pre class="lang-rust prettyprint-override"><code>struct Days(i64);
  struct Years(i64);
  
  fn old_enough(age: &amp;Years) -&gt; bool {
      age.0 &gt;= 18
  }
  </code></pre>
  <p>This is different from a <code>typedef</code> or <code>using</code> declaration in C++ which simply renames the type.<br />
  For example the <code>old_enough</code> function below would accept an <code>int</code>, an age in <code>Days</code>, or anything else that converts to an <code>int</code>:</p>
  <pre class="lang-cpp prettyprint-override"><code>typedef int Days;
  using Years = int;
  
  bool old_enough(Years age) {
      return age &gt;= 18;
  }
  </code></pre>
  <p>As the example above just uses integers, <a href="https://www.reddit.com/r/cpp/comments/2smwx1/type_safe_identifiers_in_c/" rel="noreferrer">this</a> post on Reddit suggests using enum classes, e.g.:</p>
  <pre class="lang-cpp prettyprint-override"><code>enum class Days : int {};
  enum class Years : int {};
  
  bool old_enough(Years age) {
      return static_cast&lt;int&gt;(age) &gt;= 18;
  }
  </code></pre>
  <p>Or it could simply use structures, like Rust e.g.:</p>
  <pre class="lang-cpp prettyprint-override"><code>struct Days final {int value;};
  struct Years final {int value;};
  
  bool old_enough(Years age) {
      return age.value &gt;= 18;
  }
  </code></pre>
  <p>What is the best way to implement the <code>newtype</code> idiom in <code>C++</code>?<br />
  Is there a standard method?</p>
  <p><strong>EDIT</strong> the question <a href="https://stackoverflow.com/questions/34287842/strongly-typed-using-and-typedef">Strongly typed using and typedef</a> is similar. However, it does not consider the <code>newtype</code> idiom.</p>