title: implicit conversions from and to class types
body: <p>I am studying converting constructors and conversion operators in C++.
  What I've learned so far is that any non-explicit constructor that takes only one parameter (and any number of optional default arguments) represents an implicit class-type conversion to THAT class type, for example if a class defines a constructor that has one parameter of type <code>int</code> I can use an <code>int</code> wherever an object of that class type is required:</p>
  
  <p>(assuming class_type has an overloaded += operator) </p>
  
  <pre><code>class_type a;
  a+=5;
  </code></pre>
  
  <p>in this case 5 is implicitly converted (through the converting constructor) to <code>class_type</code>and the overloaded operator is called.</p>
  
  <p>Now, the (at least for me) tricky part: I know I can define a conversion operator as a member function :</p>
  
  <pre><code>operator int() {....};
  </code></pre>
  
  <p>that converts the object of <code>class_type</code> to the primitive <code>int</code> type, and I can use that conversion like:</p>
  
  <pre><code>class_type a;
  a+5;
  </code></pre>
  
  <p>in this case I've read that the object is converted to an int through its conversion operator and then the buil-in sum operator is called.
  But what if I defined an overloaded + operator to take two class_type objects as its arguments? something like </p>
  
  <p><code>class_type operator+(const class_type&amp;,const class_type &amp;c);</code></p>
  
  <p>how is the compiler supposed to know which one to call through function matching?
  does the conversion to <code>int</code> only happens implicitly when only the built-in operator is defined?</p>
  
  <p>thanks!</p>
  
  <p>edit:</p>
  
  <p>actually,I've tried to write some code to effectively try it out, it turned out that my compiler (g++) doesn't issue any ambiguous call error!</p>
  
  <p>this is the class header (along with the non-memeber operator+ function declaration) :</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class wrapper {
      friend std::ostream &amp;operator&lt;&lt;(std::ostream&amp;,const wrapper&amp;);
    public:
      wrapper()=default; 
      wrapper(int);
      int get();
      operator int() const;
      wrapper operator+(int);
    private:
      int a=10;
  };
  
  std::ostream &amp;operator&lt;&lt;(std::ostream&amp;,const wrapper&amp;);
  </code></pre>
  
  <p>and this is the main code:</p>
  
  <pre><code>#include "wrapper.h"
  
  int main()
  {
    using namespace std;
    wrapper w1;
    wrapper w2(5);
    cout&lt;&lt;w1&lt;&lt;" "&lt;&lt;w2&lt;&lt;endl;
    w1+1;  
  }
  </code></pre>
  
  <p>now,I've defined a converting constructor from <code>int</code> to <code>wrapper</code> AND a conversion operator from class type to <code>int</code>(I've also overloaded the &lt;&lt; output operator in order to print some results), but when the compiler evaluates the expression <code>w1+1</code> it seems to be fine. How could it possibly be??</p>