c++ - 如何创建不可复制元素的容器

标签 c++ stl containers noncopyable

有没有办法使用带有不可复制元素的 STL 容器?

是这样的:

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++ 容器类中。

根据标准,23.1 第 3 段,“这些组件中存储的对象类型必须满足 CopyConstructible 类型(20.1.3)的要求,以及 Assignable 的附加要求 类型。”

关于c++ - 如何创建不可复制元素的容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1440287/

相关文章:

c++ - Netbeans 突出显示没有错误的错误

c++ - 什么是裸指针?

STL - gtest:体系结构 x86_64 的 undefined symbol 与 clang++ 和 std::vector 错误

redis - Kubernetes集群中如何连接redis-ha集群?

c++ - 段错误,没有核心转储

c++ - 对 C++ 中的非纯虚类感到困惑,它们有什么用?

c++ - 有人可以向我解释每段代码吗,我不明白,emplace_hint() 函数如何工作并加快插入过程

c++ - std::back_insert_iterator 可以用在 std::ostream 上吗?

javascript - 多次滚动容器 div

c++ - 如何编码传出引用以使用 unique_ptr 在容器上搜索