title: Conversion operator causing compilation error
body: <p>I have implemented a string-like class, called Str, and want to add a conversion to bool type, in order to use a Str object as a condition. </p>
  
  <p>The Str class is quite simple and relies on another template class called Vec, which is similar to the STL vector class. Below I have copied the main parts of the definition</p>
  
  <pre><code>class Str
  {
  
      friend std::istream&amp; operator&gt;&gt;(std::istream&amp;, Str&amp;);
  
  private:
      Vec&lt;char&gt; data;
  
  public:
      typedef Vec&lt;char&gt;::size_type size_type;
      typedef Vec&lt;char&gt;::iterator iterator;
      typedef Vec&lt;char&gt;::const_iterator const_iterator;
  
      iterator begin() { return data.begin(); }
      const_iterator begin() const { return data.begin(); }
      iterator end() { return data.end(); }
      const_iterator end() const { return data.end(); }
  
      //[...]
  
      char&amp; operator[](size_type i) { return data[i]; }
      const char&amp; operator[](size_type i) const { return data[i]; }
      Str&amp; operator+=(const Str&amp; s)
      {
          std::copy(s.data.begin(), s.data.end(), std::back_inserter(data));
          return *this;
      }
  
  };
  
  Str::operator bool() const
  {
      if (data.size() &gt; 0)
          return true;
      return false;
  }
  </code></pre>
  
  <p>Everything works fine, except that now, if I try for example to do the following:</p>
  
  <pre><code>Str greeting = "Hello, " + name + "!";
  </code></pre>
  
  <p>then I get a compilation error, saying "'operator +': 2 overloads have similar conversions, could be 'Str operator +(const Str &amp;,const Str &amp;)' or 'built-in C++ operator+(const char [8], int)'. </p>
  
  <p>I understand the reason for the error, but I am not sure how to fix it. How can I get around this?</p>
  
  <p>Thanks.</p>
  
  <p>EDIT: below I have added a minimal, complete and verifiable example:</p>
  
  <pre><code>#include &lt;iterator&gt;
  #include &lt;vector&gt;
  #include&lt;iostream&gt;
  
  using namespace std;
  
  class Str
  {
  
  private:
      std::vector&lt;char&gt; data;
  
  public:
      typedef std::vector&lt;char&gt;::size_type size_type;
      typedef std::vector&lt;char&gt;::iterator iterator;
      typedef std::vector&lt;char&gt;::const_iterator const_iterator;
  
      iterator begin() { return data.begin(); }
      const_iterator begin() const { return data.begin(); }
      iterator end() { return data.end(); }
      const_iterator end() const { return data.end(); }
  
  
      // default constructor must be defined explicitly, since non-default constructors are also defined
      Str() { }
      Str(const size_type n, const char c) : data(n, c) {  }
      Str(const char* cp)
      {
          std::copy(cp, cp + std::strlen(cp), std::back_inserter(data));
      }
      // Since this is a template, the iterators can be anything: pointers in an array of chars, std::vector&lt;string&gt; iterators, std::std::vectortor&lt;string&gt; iterators, std::list&lt;string&gt; iterators, etc.
      template &lt;class In&gt; Str(In b, In e)
      {
          std::copy(b, e, std::back_inserter(data));
      }
      char&amp; operator[](size_type i) { return data[i]; }
      const char&amp; operator[](size_type i) const { return data[i]; }
      Str&amp; operator+=(const Str&amp; s)
      {
          std::copy(s.data.begin(), s.data.end(), std::back_inserter(data));
          return *this;
      }
      size_type size() const
      {
          return data.size();
      }
  
      // conversion operators
      operator std::vector&lt;char&gt;() const;
      operator bool() const;
  
      template &lt;class In&gt; void insert(iterator dest, In b, In e)
      {
          data.insert(dest, b, e);
      }
  
  };
  
  Str operator+(const Str&amp; s1, const Str&amp; s2)
  {
      Str s = s1;
      s += s2;
      return s;
  }
  
  Str::operator std::vector&lt;char&gt;() const
  {
      std::vector&lt;char&gt; ret = data;
      return ret;
  
  }
  
  Str::operator bool() const
  {
      if (data.size() &gt; 0)
          return true;
      return false;
  }
  
  
  int main()
  
  {
  
      Str name = "Joe";
      Str greeting = "Hello, " + name + "!";
  
      return 0;
  }
  </code></pre>