Why does writing to temporary stream fail?

前端 未结 1 1025
独厮守ぢ
独厮守ぢ 2020-12-11 05:19

Consider the following code:

#include 
#include 

class Foo : public std::stringstream {
public:
    ~Foo() { std::cout <&l         


        
相关标签:
1条回答
  • 2020-12-11 05:46

    I tested it.

    I can guess that operator<< cannot bind a temporary to a non-const reference, so any externally defined operator<< functions will not work on the Foo temporary, but any class member ones will so if ostream or ostringstream has any internal operator<< members they will work.

    Therefore it may be that the overload to a pointer is a member function whilst the special one for const char * is externally declared.

    The non-temporary can bind to the non-const reference for the more specialist overload.

    If you really need this you can workaround with a wrapper

    class Foo :
    {
        mutable std::ostringstream oss;
    public:
      ~Foo()
      {
        std::cout << oss.str();
      }
    
      template<typename T>
      std::ostream&
      operator<<( const T& t ) const
      {
          return oss << t;
      }
    };
    

    Tested and works. The first operator<< will return you the underlying stream.

    I tried this too but it coredumped:

    class Foo : std::ostringstream
    {
        Foo & nonconstref;
    public:
       Foo() : nonconstref( *this ) {}
      ~Foo()
      {
        std::cout << str();
      }
    
      template<typename T>
      std::ostream&
      operator<<( const T& t ) const
      {
          return nonconstref << t;
      }
    };
    

    This also works:

    class Foo : public std::ostringstream
    {
    public:
       Foo()  {}
      ~Foo()
      {
        std::cout << str();
      }
    
      Foo& ncref()
      {
           return *this;
      }
    };
    
    int main()
    {
        Foo foo;
        foo << "Test1" << std::endl;
    
        Foo().ncref() << "Test2" << std::endl;
    
    }
    
    0 讨论(0)
提交回复
热议问题