【发布时间】:2011-10-10 22:01:10
【问题描述】:
考虑下面的代码。
#include <iostream>
#include <string>
struct SimpleStruct
{
operator std::string () { return value; }
std::string value;
};
int main ()
{
std::string s; // An empty string.
SimpleStruct x; // x.value constructed as an empty string.
bool less = s < x; // Error here.
return 0;
}
此代码不能在 g++ 或 Microsoft Visual C++ 上编译。编译器给出的错误报告是no match for operator '<' in 's < x'。问题是为什么编译器不简单地将SimpleStruct x 转换成string 根据给定的operator string () 再使用operator < ( string, string )?
【问题讨论】:
标签: c++ operators type-conversion