2014-01-16 13 views
2

我正在开发一个重载流插入操作符的记录器类。 我无法捕捉std::flush操纵器。std :: flush操作函数的地址不等于以前存储在静态const变量中的地址

首先,我做的一个简单的总结:

鉴于LogClass一个对象,我要做到以下几点:

LogClass logger; 
logger << "Some text" << std::flush; 

...,赶上std::flush操纵。

所有输入直接通过下面的内嵌运营商(其中正常工作)发送到内部字符串流,用于以后的日志记录:

class LogClass 
{ 
    // ... 

    std::ostringstream m_internalStream; 

    template <typename T> 
    LogClass & operator<<(const T & rhs) 
    { 
     m_internalStream << rhs; 
     return *this; 
    } 

    // ... 
}; 

我试图赶上std::flush机械手通过重载如下(这也正常工作):

LogClass & LogClass::operator<<(std::ostream & (*manip)(std::ostream &)) 
{ 
    std::ostream & (* const flushFunc)(std::ostream &) = std::flush; 

    // Is the supplied manipulator the same as std::flush? 
    if ((manip == flushFunc)) { 
     flush(); // <-- member function of LogClass 
    } else { 
     manip(m_stream); 
    } 

    return *this; 
} 

的问题表明,如果我尝试做局部变量flushFunc静态的,如下:

static std::ostream & (* const flushFunc)(std::ostream &) = std::flush; 

在这种情况下,输入manip指针的值是而不是等于flushFunc

这是为什么?与charstd::flush函数的模板实例有什么关系?

我正在使用MS Visual Studio 2010 Pro。

的问题也显示出在这个狭小的工作代码片段:

#include <iostream> 
#include <iomanip> 

int main(int, char **) 
{ 
    static std::ostream & (* const staticFlushPtr)(std::ostream &) = std::flush; 
    std::ostream & (* const stackFlushPtr)(std::ostream &) = std::flush; 

    std::cout << std::hex << 
      "staticFlushPtr: " << (void *) staticFlushPtr << "\n" 
      "stackFlushPtr: " << (void *) stackFlushPtr << "\n"; 

    return 0; 
} 

...这使我的机器上输出:

staticFlushPtr: 013F1078 
stackFlushPtr: 0FF10B30 

任何想法?

最好的问候, 赖A. Apeland

回答

1

这看起来像编译器中的一个明确的错误。如果有 DLL或参与这样的,我能理解它,或者至少可以 借口,但在这样一个简单的例子如您main ...(G ++ 得到它的权利。)

所有我可以建议是你在你的输出中插入了一个过滤streambuf ,它可以调用sync

+0

我同意。只需将其编译在MAC和在线编译器上([link](http://www.compileonline.com/compile_cpp11_online.php))即可。在这两种情况下,指针值都是相等的。 – raapeland