为什么不能构造包含ostringstream成员的对象? [英] Why can't an object containing a ostringstream member be constructed?

查看:253
本文介绍了为什么不能构造包含ostringstream成员的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程示例,是从较大的项目简化而来的.它基于一个日志记录框架,该框架使用记录器的作用域终止析构函数中的日志条目.

I have the following class example, simplified from a larger project. It's based on a logging framework that uses the logger's scope to terminate a log entry in the destructor.

下面的代码将无法编译,因为构造函数是一个隐式删除的函数( edit:不是true ),该函数似乎与std::ostringstream对象有关.我对此感到困惑,因为我认为我应该能够直接构造std::ostringstream,这意味着我应该能够直接构造Container对象.

The code below will not compile because the constructor is an implicitly deleted function (edit: not true), which seems to have something to do with the std::ostringstream object. I'm confused about that because I think I should be able to directly construct a std::ostringstream, which would mean I should be able to directly construct a Container object.

#include <iostream>
#include <sstream>

class Container {
  public:
    std::ostringstream  bufferStream;

  public:
    Container();    // constructor
    ~Container();
};

Container::Container() {
    bufferStream << "Hello ";
}

Container::~Container() {
    std::cout << bufferStream.str() << " [end]" << std::endl;
}

// === Main method ===

int main() {

    Container().bufferStream << "world";   // works fine

    {                                      // causes tons of compiler errors
        Container cont = Container();
        cont.bufferStream << "world!";
    }

    return 0;
}

请注意,标记为工作正常"的行就是这样做的.似乎实例化了一个匿名Container对象,该对象包含一个新的std::ostringstream,可以直接访问该对象以输出世界". Container本身创建消息的"Hello"部分,其析构函数刷新缓冲区.

Note that the line labeled "works fine" does just that. It seems to instantiate an anonymous Container object, which contains a new std::ostringstream, which can be directly accessed to output "world". The Container itself creates the "Hello" part of the message and its destructor flushes the buffer.

为什么命名和保存Container对象的第二部分不能正确运行?这是我得到的错误的一个示例:

Why doesn't the second part, in which the Container object is named and saved, run correctly? Here is a sample of the errors I get:

error.cpp: In function ‘int main()’:
error.cpp:28:36: error: use of deleted function ‘Container::Container(const Container&)’
         Container cont = Container();
                                    ^
error.cpp:4:7: note: ‘Container::Container(const Container&)’ is implicitly deleted because the default definition would be ill-formed:
 class Container {
       ^
error.cpp:4:7: error: use of deleted function ‘std::basic_ostringstream<char>::basic_ostringstream(const std::basic_ostringstream<char>&)’
In file included from error.cpp:2:0:
/usr/include/c++/4.8/sstream:387:11: note: ‘std::basic_ostringstream<char>::basic_ostringstream(const std::basic_ostringstream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
     class basic_ostringstream : public basic_ostream<_CharT, _Traits>

...等等.

推荐答案

这会很好:

Container cont;
cont.bufferStream << "world!";

但是这个:

Container cont = Container();

涉及复制构造函数. std::ostringstream不可复制构造,这使得Container不可复制构造,因此,由于std::basic_ostringstream<char>::basic_ostringstream(const std::basic_ostringstream<char>&)被隐式删除,因此错误消息说明了如何Container::Container(const Container&)被隐式删除.

involves the copy constructor. std::ostringstream is not copy-constructible which makes Container not copy-constructible, hence the error message talking about how Container::Container(const Container&) is implicitly deleted due to std::basic_ostringstream<char>::basic_ostringstream(const std::basic_ostringstream<char>&) being implicitly deleted.

请注意,即使此副本将被删除,但复制/移动省略的要求是必须能够开始复制/移动.

Note that even though this copy would be elided, a requirement of copy/move elision is that the copy/move must be possible to begin with.

这篇关于为什么不能构造包含ostringstream成员的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆