title: Conversion is ambiguous. Standard implicit conversion could not choose cast operator
body: <p>I have a custom type described as </p>
  
  <pre><code>struct A {
      double dbl_;
      bool boo_;
      operator double() const { return dbl_; }
      //operator bool() const { return boo_; }
  };
  </code></pre>
  
  <p>And now I want to cast it to simple types. When <code>operator bool()</code> is undefined <code>a</code> can be implicitly converted to any simple type int, unsigned, float, etc. But with <code>operator bool()</code> conversion is ambiguous.</p>
  
  <pre><code>A a;
  cout &lt;&lt; (double) a &lt;&lt; endl;
  cout &lt;&lt; (float) a &lt;&lt; endl; //error: conversion from 'A' to 'float' is ambiguous; candidates are: A::operator bool() const; A::operator double() const
  cout &lt;&lt; (int) a &lt;&lt; endl;  // the same
  cout &lt;&lt; (char) a &lt;&lt; endl; // the same
  return 0;
  </code></pre>
  
  <p>Runnable code on <a href="http://cpp.sh/3scp" rel="nofollow noreferrer">cpp.sh</a>  </p>
  
  <p><strong>I know a few ways to fix that:</strong>  </p>
  
  <p>1.add type conversion operators for <strong>all</strong> the expected types.</p>
  
  <pre><code> operator int() const { return (int)dbl_; }
   // and so on...
  </code></pre>
  
  <p>This looks like bad practice.</p>
  
  <p>2.use <em><a href="https://stackoverflow.com/questions/35242266/how-to-restrict-typenames-in-template">template with restricted types</a></em>.  </p>
  
  <pre><code>template&lt;class T, class...&gt; struct is_any_of: std::false_type{};
  template&lt;class T, class Head, class... Tail&gt;
  struct is_any_of&lt;T, Head, Tail...&gt; : std::conditional&lt;
          std::is_same&lt;T, Head&gt;::value,
          std::true_type,
          is_any_of&lt;T, Tail...&gt; &gt;::type
  {};
  
  template&lt;
          class T,
          class = typename std::enable_if&lt;is_any_of&lt;T, int, float, unsigned, double&gt;::value&gt;::type
  &gt;
  operator T() const {
      if(type_ != Type::NUMBER) throw Node::Exception("not is number");
      return dbl_;
  }
  </code></pre>
  
  <p>3.Hold <code>bool</code> value in <code>dbl_</code>, because only one of them is used. Not cool, as for me.</p>
  
  <p>May be a more refined solution exists? Like</p>
  
  <pre><code>operator bool() const no_implicit_conversation_to_other_types_specifier { return boo_; }
  </code></pre>
  
  <p>The question is at most theory of C++.</p>
  
  <p><strong>Upd.</strong> no_implicit_conversation_to_other_types_specifier is <code>explicit</code></p>
  
  <pre><code>explicit operator bool() const { return boo_; }
  </code></pre>
  
  <h2><a href="http://cpp.sh/4l4iv" rel="nofollow noreferrer">Run</a>.</h2>