This is my test case (note the WTF comment):
TEST(string_assignment)
{
std::string str;
std::string cheese="Cheese";
str=cheese;
CHECK_EQUAL(cheese, str);
long lval=0;
str=lval; //WTF - why does the compiler allow this ?
str="";
str.append(cheese);
CHECK_EQUAL(cheese, str);
}
I want to catch instances of std::string being assigned something other than another string or a char*. I had assumed that the compiler would reject the incompatible type but it is allowing it.
How can I tell gcc (version 4.4.3) to reject this silliness ? … or is there some other way to force rejection of these incompatible types being assigned to std::string ?
The reason is that the following overload exists:
The compiler can satisfy your assignment with a single implicit conversion, so it compiles.
I think the
-WconversionGCC flag is supposed to deal with this, but it doesn’t seem to work, at least in GCC 4.1.2.