如何创建不可复制元素的容器 [英] How to create a container of noncopyable elements

查看:186
本文介绍了如何创建不可复制元素的容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用STL包含不可复制的元素?

Is there a way use STL containters with non-copyable elements?

这样:

class noncopyable
{
    noncopyable(noncopyable&);
    const noncopyable& operator=(noncopyable&);
public:
    noncopyable(){};
};

int main()
{
    list<noncopyable> MyList; //error C2248: 'noncopyable::noncopyable' : cannot access private member declared in class 'noncopyable'
}


推荐答案

否,不可复制的元素不能在C ++容器类中。

No, non-copyable elements can't be in C++ container classes.

,23.1第3段存储在这些组件中的对象类型必须满足 CopyConstructible types(20.1.3)的要求,以及可分配类型。

According to the standard, 23.1 paragraph 3, "The type of objects stored in these components must met the requirements of CopyConstructible types (20.1.3), and the additional requirements of Assignable types."

这篇关于如何创建不可复制元素的容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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