C++: namespace resolution bug

Simpson, Kenny Kenny.Simpson@gs.com
Mon Jan 22 14:48:00 GMT 2001


Hi,
  I'm having problems with function overloading across namespaces.
Code which was not mine had a class that publicly inherited from
std::string.
Here is a simplified test case: (std:: -> bar:: , string -> Old_String )

namespace bar
{
  struct Old_String
  {
    Old_String( char const* ) {} // dummy
    Old_String& append( Old_String const& ) { return *this;} // dummy
  };

  Old_String operator+( char const* a, Old_String const& b )
  {
    return Old_String( a ).append( b );
  }
}

namespace foo
{
  struct My_String : public bar::Old_String
  {
    My_String( char const* str ) : bar::Old_String( str ) {}
    MY_String& operator+=( My_String const& rhs ) { return a += b; }
  };

  My_String operator+( My_String a, My_String const& b ) { return a += b; }
}

namespace foo
{
  void func()
  {
    My_String s1 = " hi " + My_String( " world " ); // <-- causes error
  }
}

int main()
{
  foo::func();
  return 0;
}


The problem, as far as I can tell, is that in trying to resolve
" hi " + MyString( " world " ), foo::operator+ is found, and bar::operator+
is found.
Something goes bad, and instead of rejecting bar::operator+ as a candidate,
I get
an error:
  conversion from `bar::Old_String' to non-scalar type
    `foo::My_String' requested

If I comment out the bar::operator+, the code compiles fine.
This happens with gcc-2.95.2/20001211/20010120.

Thanks,
-Kenny

(not speaking for my employer)


More information about the Gcc-bugs mailing list