使用GNU g ++ 4.9.2时,在以下代码段的编译中出现问题(用于在g ++ 2.95.3中编译确定)
XOStream &operator<<(ostream &(*f)(ostream &)) {
if(f == std::endl) {
*this << "\n" << flush;
}
else {
ostr << f;
}
return(*this);
}
错误如下:
error: assuming cast to type 'std::basic_ostream<char>& (*)(std::basic_ostream<char>&)' from overloaded function [-fpermissive]
[exec] if(f == std::endl) {
[exec] ^
请指导/帮助。
std::endl
用来选择的重载static_cast
:
#include <iostream>
#include <iomanip>
inline bool is_endl(std::ostream &(*f)(std::ostream &)) {
// return (f == static_cast<std::ostream &(*)(std::ostream &)>(std::endl));
// Even nicer (Thanks M.M)
return (f == static_cast<decltype(f)>(std::endl));
}
int main()
{
std::cout << std::boolalpha;
std::cout << is_endl(std::endl) << '\n';
std::cout << is_endl(std::flush) << '\n';
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句