看看下面大小写转换的c++推荐代码为什么只能在vc下编译成功,而gcc下出问题呢?
#include <cctype> // for toupper #include <string> #include <algorithm> using namespace std; void main() { string s="hello"; transform(s.begin(), s.end(), s.begin(), toupper); }
看看如何解决?
Alas, the program above will not compile because the name 'toupper' is ambiguous. It can refer either to:
int std :: toupper ( int ); // from <cctype>
or
template < class chart >
charT std :: toupper ( charT , const locale &); // from <locale>
Use an explicit cast to resolve the ambiguity:
std::transform(s.begin(), s.end(), s.begin(), (int(*)(int)) toupper);
This will instruct the compiler to choose the right toupper().
貌似是std下的toupper函数的重载,gcc的判决fail了。但vc2005等会自动处理。
引用资料:
http://www.megasolutions.net/cplus/GCC-MSVC++-difference-32501.aspx
这个可能解释了原因,其中有一段话:
Right. The problem you face is that VC++ conforms better to the C++
Standard than does GCC. The former provides the required overloads
for exp; the latter is at the mercy of the underlying C library to
provide those overloads, and certainly glibc fails to do so. By
interposing the wrapper function, you disambiguate the call to
transform.
HTH,
P.J. Plauger
Dinkumware, Ltd.