title: Implementing expected<E, T> according to proposal n4015
body: <p>I started implementing <code>expected&lt;E, T&gt;</code> according to the proposal and I ran into a problem.
  When describing the monadic functionality (5.9) it is stated in bind that if the result is already wrapped in context (<code>expected&lt;E, T&gt;</code>) it shouldn't be wrapped again.</p>
  
  <p>How I implemented it is that bind had 2 overloads (with <code>enable_if</code>), one for functors that return an instantiation of <code>expected</code> which the implementation didn't wrap it in context and one for those that returned other types which it did. I ran into a problem where the non-wrapping version, in the absence of a value in the caller had to return a default constructed <code>expected&lt;E1, T1&gt;</code> which gave the expected a default constructed error value.</p>
  
  <p>That caused further continuations of the returned expected to lose the context of the error that caused the default construction.</p>
  
  <p>Perhaps I should treat the context which was referred to as <code>is_same&lt;expected&lt;E,T&gt;, functor_ret_type&gt;</code> and here in case of an error in the caller I can forward "this" and keep the context of the error, but that would cause functors returning <code>expected&lt;E1, E2&gt;</code> to return <code>expected&lt;E, expected&lt;E1, E2&gt;&gt;</code></p>
  
  <p>Assuming the following about the class:</p>
  
  <pre><code>E&amp; error(); //returns the stored error
  T&amp; operator*(); //returns a reference to the stored value
  bool has_value(); //returns true if the expected value is present, false if the error is present
  </code></pre>
  
  <p>Here is how I had it implemented at first:</p>
  
  <pre><code>//overload for wrapping functors
  template&lt;typename Functor&gt;
  auto bind(Functor functor) -&gt; std::enable_if&lt;is_instance&lt;expected, 
                                               decltype(functor(**this))&gt;::value,
                                               decltype(functor(**this))&gt;::type
  {
      using result_type = decltype(functor(**this));
      if (this-&gt;has_value())
      {
          return functor(**this);
      }
      return result_type();
  }
  
  
  //overload for non wrapping functors
  template&lt;typename Functor&gt;
  auto bind(Functor functor) -&gt; std::enable_if&lt;!is_instance&lt;expected, 
                                               decltype(functor(**this))&gt;::value,
                                               expected&lt;E, decltype(functor(**this))&gt;&gt;::type
  {
      using result_type = decltype(functor(**this));
      if (has_value())
      {
          return { functor(**this) };
      }
      return expected&lt;E, result_type&gt;(this-&gt;error());
  }
  </code></pre>
  
  <p>Here you can see that in the first overload, if we have no value, we return a default constructed <code>expected&lt;E, T&gt;</code> of the type that <code>functor</code> returns, which holds a default constructed error type <code>E</code>, which makes us lose the context of the original error due to which functor wasn't called.</p>
  
  <p>If I am to interpret the described behavior as "if functor returns <code>expected&lt;E, T&gt;</code> of the same type as the one which the bind was called for don't wrap otherwise wrap" we can have the following implementation:</p>
  
  <pre><code>//overload for wrapping functors
  template&lt;typename Functor&gt;
  auto bind(Functor functor) -&gt; std::enable_if&lt;is_same&lt;expected&lt;E, T&gt;, 
                                               decltype(functor(**this))&gt;::value,
                                               decltype(functor(**this))&gt;::type
  {
      if (this-&gt;has_value())
      {
          return functor(**this);
      }
      return *this;
  }
  
  
  //overload for non wrapping functors
  template&lt;typename Functor&gt;
  auto bind(Functor functor) -&gt; std::enable_if&lt;!is_same&lt;expected&lt;E, T&gt;,
                                               decltype(functor(**this))&gt;::value,
                                               expected&lt;E, decltype(functor(**this))&gt;&gt;::type
  {
      using result_type = decltype(functor(**this));
      if (has_value())
      {
          return { functor(**this) };
      }
      return expected&lt;E, result_type&gt;(this-&gt;error());
  }
  </code></pre>
  
  <p>Here, if no value is present in the wrapping overload, we forward <code>this</code> which allows us to know what was the initial error, but if functor returns <code>expected&lt;E1, T1&gt;</code> the result would be a wrapped <code>expected&lt;E, expected&lt;E1, T1&gt;&gt;</code></p>
  
  <p>A third approach I can think of is only treat <code>E</code> as the context, which would be a little less restrictive, and would allow to unwrap a functor which returns <code>expected&lt;E, T1&gt;</code>, but the problem for <code>expected&lt;E1, T1&gt;</code> is still present.</p>
  
  <p>Which is the correct approach? Maybe there is another approach that I missed altogether?</p>
  
  <p>Thank you.</p>