title: How can I prevent integer ambiguity when overloading operators in C++
body: <p>Given the following code:</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;sstream&gt;
  
  class SomeClass {
  public:
      SomeClass&amp; operator&lt;&lt;(long value) { return *this; }
  
      SomeClass&amp; operator&lt;&lt;(unsigned long value) { return *this; }
  
      SomeClass&amp; operator&lt;&lt;(float value) { return *this; }
  
      SomeClass&amp; operator&lt;&lt;(double value) { return *this; }
  
      SomeClass&amp; operator&lt;&lt;(long double value) { return *this; }
  
      SomeClass&amp; operator&lt;&lt;(bool value);
      { return *this; }
  
      SomeClass&amp; operator&lt;&lt;(const void* value);
      { return *this; }
  
      SomeClass&amp; operator&lt;&lt;(::std::string aString) { return *this; }
  };
  
  int main() {
      SomeClass aClass;
      std::ostringstream aStream;
  
      aClass &lt;&lt; 2;
      aClass &lt;&lt; "Hello World";
      aClass &lt;&lt; '\0';
      aClass &lt;&lt; -2;
  
      aStream &lt;&lt; 2;
      aStream &lt;&lt; "Hello World";
      aStream &lt;&lt; '\0';
      aStream &lt;&lt; -2;
  
      return 0;
  }
  </code></pre>
  
  <p>Why is <code>basic_ostream</code> able to overload <code>&lt;&lt;</code> with so many integer types with no problem? Yet, the compiler is giving me  ambiguous overload errors for SomeClass's &lt;&lt; operators?</p>
  
  <p>ex:</p>
  
  <pre><code>../OverloadTest.cpp:65: error: ambiguous overload for ‘operator&lt;&lt;’ in ‘aClass &lt;&lt; 2’
  ../OverloadTest.cpp:14: note: candidates are: SomeClass&amp; SomeClass::operator&lt;&lt;(long int)   
  ../OverloadTest.cpp:19: note:                 SomeClass&amp; SomeClass::operator&lt;&lt;(long unsigned int)
  ../OverloadTest.cpp:24: note:                 SomeClass&amp; SomeClass::operator&lt;&lt;(float)    
  ../OverloadTest.cpp:29: note:                 SomeClass&amp; SomeClass::operator&lt;&lt;(double)    
  ../OverloadTest.cpp:35: note:                 SomeClass&amp; SomeClass::operator&lt;&lt;(long double)    
  ../OverloadTest.cpp:40: note:                 SomeClass&amp; SomeClass::operator&lt;&lt;(bool)    
  ../OverloadTest.cpp:46: note:                 SomeClass&amp; SomeClass::operator&lt;&lt;(const void*)     
  ../OverloadTest.cpp:51: note:                 SomeClass&amp; SomeClass::operator&lt;&lt;(std::string) 
  </code></pre>