2

I'm attempting to build a bit of simple logging functionality from scratch, with a stream like '<<' interface, and am running into a bit of compiler issue. Here is the basics of my code:

#include <string.h>
#include <sstream>
#include <iostream>

class Logger
{
public:
    class Buffer
    {
    public:
        Buffer(Logger &parent) : mPar(parent)
        {
        }

        ~Buffer(void)
        {
            mPar.endline(os);
        }

        template<class T>
        Buffer& operator<<(const T& x)
        {
            os << x;
            return *this;
        }

        Logger& mPar;
        std::ostringstream os;
    };

    Buffer write(void)
    {
        return Buffer(*this);
    }

    void endline(std::ostringstream& os)
    {
        std::cout << os.str() << std::endl;
    }
};

int main(int argc, char **argv)
{
    Logger log;

    log.write() << "fred" << 3 << "bob";
}

The errors I'm getting are:

In file included from /usr/include/c++/4.6/ios:45:0,
                 from /usr/include/c++/4.6/istream:40,
                 from /usr/include/c++/4.6/sstream:39,
                 from test.cpp:2:
/usr/include/c++/4.6/bits/ios_base.h: In copy constructor ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’:
/usr/include/c++/4.6/bits/ios_base.h:788:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/include/c++/4.6/bits/basic_ios.h:64:11: error: within this context
In file included from test.cpp:2:0:
/usr/include/c++/4.6/sstream: In copy constructor ‘std::basic_ostringstream<char>::basic_ostringstream(const std::basic_ostringstream<char>&)’:
/usr/include/c++/4.6/sstream:373:11: note: synthesized method ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ first required here
/usr/include/c++/4.6/streambuf: In copy constructor ‘std::basic_stringbuf<char>::basic_stringbuf(const std::basic_stringbuf<char>&)’:
/usr/include/c++/4.6/streambuf:782:7: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const __streambuf_type&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_streambuf<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]’ is private
/usr/include/c++/4.6/sstream:60:11: error: within this context
/usr/include/c++/4.6/sstream: In copy constructor ‘std::basic_ostringstream<char>::basic_ostringstream(const std::basic_ostringstream<char>&)’:
/usr/include/c++/4.6/sstream:373:11: note: synthesized method ‘std::basic_stringbuf<char>::basic_stringbuf(const std::basic_stringbuf<char>&)’ first required here
test.cpp: In copy constructor ‘Logger::Buffer::Buffer(const Logger::Buffer&)’:
test.cpp:8:8: note: synthesized method ‘std::basic_ostringstream<char>::basic_ostringstream(const std::basic_ostringstream<char>&)’ first required here
test.cpp: In member function ‘Logger::Buffer Logger::write()’:
test.cpp:33:22: note: synthesized method ‘Logger::Buffer::Buffer(const Logger::Buffer&)’ first required here

From what I've been able to find so far, the errors are because you can't call the copy constructor on ostringstream. As far as I can tell though, I'm not calling a copy constructor directly, and I'm not copying Buffer, only constructing it in the 'return' statement.

The other interesting thing is that this code compiles fine under Visual Studio 2010 where I knocked it up before integrating it into my application (which is compiled using GCC 4.6.3).

Have I interpreted the issue correctly, and if so - where is the implicit copy, and how do eliminate it?

4

1 回答 1

6

第一个问题是std::ostringstream不可复制的,您在创建Buffer.

Buffer您在此return声明中创建副本的原因:

return Buffer(*this);

是您的类Buffer没有隐式生成的移动构造函数。而它之所以没有,是因为它Buffer有一个用户定义的析构函数,它禁止隐式生成移动构造函数。

您可以明确定义一个:

    Buffer(Buffer&& b)
        :
        mPar(b.mPar),
        os(std::move(b.os))
    {
    }

但是你应该知道标准库的一些实现并不完全兼容,并且没有实现std::ostringstream.

于 2013-04-21T13:47:32.997 回答