Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mystr.append("abcd" + ss) bug

Status
Not open for further replies.

modemer

Technical User
Feb 16, 2005
22
US
Anybody could explain this:

On Sun 10, CC version is Sun C++ 5.7 2005/01/07

I have this code:

string mystr("ABC"), ss("123");
mystr.append("XYZ" + ss);

The compiler doesn't print any warning on append() line, but indeed this is a bug I am currently suffering, could anybody know how the Sun's CC compiler explain the datatype of "XYZ"+ss to append()? Is this a valid syntex?

Your answers will help me to decide if I need to complain the bug to Sun.

Thanks,
M.
 
It's a legal code: why warning? Standard-conforming STL has non-member basic_string function template, informally:
Code:
string& operator +(const char* p, const string& s)
{
   return string(p) += s;
}
So this snippet result is (must be) ABCXYZ123 in mystr.
 
I think operator + is left associative operator, that means the one defined in string template doesn't work on the expression "XYZ" + ss. I think the compiler uses automatical datatype conversion on this expression instead of what you said. And probably the automatical datatype conversion has a bug in certain circumstance.
 
Works ok here - g++ version 4
Code:
$ cat foo.cpp && g++ foo.cpp && ./a.out
#include <iostream>
#include <string>
using namespace std;

int main ( ) {
  string mystr("ABC"), ss("123");
  mystr.append("XYZ" + ss);
  cout << mystr << endl;
  return 0;
}
ABCXYZ123

--
 
ABC" => const char*
ss => string& => const string&
It's exactly a case for +(const char*,const string&) signature of the (one of) STL template for string binary + operator.
It's another story what's a bag in your (Sun) compiler.
Apropos, what's a bag (and why left association issue in this obvious case)?...
Salem's snippet works OK in ancient VC++ too...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top