std::string somestring;
/*...*/
if("STRING_LITERAL" == somestring)
std::cout << "Strings are Equal" << std::endl;
Run Code Online (Sandbox Code Playgroud)
在上面的示例代码中,C++编译器如何解释==
运算符?当==运算符被字符串类重载?
如果您正在使用std::string,那么你应该有#included为<string>标题.假设是这种情况,那么所operator==选择的应该是非成员模板函数,<string>其中推导出适当的模板参数.(ISO/IEC 14882:2003 21.3.7.2 [lib.string :: operator ==])
template<class charT, class traits, class Allocator>
bool operator==(const charT* lhs,
const basic_string<charT,traits,Allocator>& rhs);
Run Code Online (Sandbox Code Playgroud)
该std::string班(严格类模板专门化)不包含任何成员重载operator==.