[英]Comparing address of std::endl
我正在检查一段现有代码,发现用Visual C ++ 9和MinGW编译时,其行为有所不同:
inline LogMsg& LogMsg::operator<<(std::ostream& (*p_manip)(std::ostream&) )
{
if ( p_manip == static_cast< std::ostream& (*)(std::ostream&) > ( &std::endl<char, std::char_traits<char> >) )
{
msg(m_output.str());
m_output.str( "" );
}
else
{
(*p_manip) (m_output); // or // output << p_manip;
}
return *this;
}
顾名思义,这是一个日志类,它重载了operator<<()
以从流中剥离Endls。
我发现了为什么它的行为有所不同:测试p_manip == static_cast...
在MinGW上成功,而在Visual C ++ 9上失败。
std::endl
的真实地址; 我将测试更改为if ( p_manip == std::endl )
,现在它的行为符合预期。
我的问题是 :如此复杂(实际上是错误的)测试背后的原理是什么?
为了完善:
class LogStream
{
public:
LogStream() {}
protected:
std::ostringstream m_output;
};
class LogMsg : public LogStream
{
friend LogMsg& msg() ;
static LogMsg s_stream;
public:
LogMsg() {}
template <typename T>
inline LogMsg& operator<<(T p_data);
inline LogMsg& operator<<(std::ostream& (*p_manip)(std::ostream&) );
};
猜测一下,我会说原始作者没有意识到它们是兼容的类型,而是按规范进行了转换(无需编译器要求他进行转换)。
有关信息:
语句if ( p_manip == std::endl )
不能在原始编译器(gcc 3.4.5,最初在其上开发代码的编译器)上编译。
这意味着测试没有错,正如我在问题中所述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.