Check if ostream object is cout or ofstream, c++

后端 未结 4 1893
北恋
北恋 2020-12-06 11:26

Is there a way in C++ to check if an ostream object is cout or a ofstream object?

Something like:

ostream& ou         


        
4条回答
  •  感动是毒
    2020-12-06 11:55

    You will definitely get further by checking the streambuffer identity

    if (s.rdbuf() == std::cout.rdbuf())
    

    This is because it is trivially simple to cross-assign / alias streams to buffers, see http://www.cplusplus.com/reference/iostream/ios/rdbuf/ and the Josuttis book

提交回复
热议问题