std::ostringstream printing the address of the c-string instead of its content

前端 未结 3 1477
青春惊慌失措
青春惊慌失措 2020-11-30 12:27

I have stumbled on a weird behavior that I just could not explain at first (see ideone):

#include 
#include 
#include 

        
3条回答
  •  难免孤独
    2020-11-30 12:47

    To get the started, the simplest solution is to get the list of possible overloads that the compiler considered, for example trying this:

    X x;
    std::cout << x << "\n";
    

    where X is a type without any overload for streaming which yields the following list of possible overloads:

    prog.cpp: In function ‘int main()’:
    prog.cpp:21: error: no match for ‘operator<<’ in ‘std::cout << x’
    include/ostream:112: note: candidates are: std::ostream& std::ostream::operator<<(std::ostream& (*)(std::ostream&))
    include/ostream:121: note:                 std::ostream& std::ostream::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&))
    include/ostream:131: note:                 std::ostream& std::ostream::operator<<(std::ios_base& (*)(std::ios_base&))
    include/ostream:169: note:                 std::ostream& std::ostream::operator<<(long int)
    include/ostream:173: note:                 std::ostream& std::ostream::operator<<(long unsigned int)
    include/ostream:177: note:                 std::ostream& std::ostream::operator<<(bool)
    include/bits/ostream.tcc:97: note:         std::ostream& std::ostream::operator<<(short int)
    include/ostream:184: note:                 std::ostream& std::ostream::operator<<(short unsigned int)
    include/bits/ostream.tcc:111: note:        std::ostream& std::ostream::operator<<(int)
    include/ostream:195: note:                 std::ostream& std::ostream::operator<<(unsigned int)
    include/ostream:204: note:                 std::ostream& std::ostream::operator<<(long long int)
    include/ostream:208: note:                 std::ostream& std::ostream::operator<<(long long unsigned int)
    include/ostream:213: note:                 std::ostream& std::ostream::operator<<(double)
    include/ostream:217: note:                 std::ostream& std::ostream::operator<<(float)
    include/ostream:225: note:                 std::ostream& std::ostream::operator<<(long double)
    include/ostream:229: note:                 std::ostream& std::ostream::operator<<(const void*)
    include/bits/ostream.tcc:125: note:        std::ostream& std::ostream::operator<<(std::basic_streambuf<_CharT, _Traits>*)
    

    First scanning this list, we can remark that char const* is conspiscuously absent, and therefore it is logical that void const* will be selected instead and thus the address printed.

    On a second glance, we note that all overloads are methods, and that not a single free function appears here.

    The issue is a problem of reference binding: because a temporary cannot bind to a reference to non-const, overloads of the form std::ostream& operator<<(std::ostream&,X) are rejected outright and only member functions remain.

    It is, as far as I am concerned, a design bug in C++, after all we are executing a mutating member function on a temporary, and this requires a (hidden) reference to the object :x

    The workaround, once you understood what went awry, is relatively simple and only requires a small wrapper:

    struct Streamliner {
      template 
      Streamliner& operator<<(T const& t) {
        _stream << t;
        return *this;
      }
    
      std::string str() const { return _stream.str(); }
      std::ostringstream _stream;
    };
    
    std::cout << "Inline, take 2: " << (Streamliner() << "some data").str() << "\n";
    

    Which prints the expected result.

提交回复
热议问题