小编use*_*736的帖子

我如何告诉gcc警告我字符串被赋予整数值?

这是我的测试用例(注意WTF评论):

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);
}
Run Code Online (Sandbox Code Playgroud)

我想捕获std :: string的实例,而不是另一个字符串或char*.我曾假设编译器会拒绝不兼容的类型,但它允许它.

我怎么能告诉gcc(版本4.4.3)拒绝这种愚蠢?...还是有其他方法强制拒绝将这些不兼容的类型分配给std :: string?

c++ string stl

2
推荐指数
1
解决办法
113
查看次数

标签 统计

c++ ×1

stl ×1

string ×1