非可复制类型的整数可索引RAII容器 [英] Integer index-able RAII container for non-copyable type

查看:127
本文介绍了非可复制类型的整数可索引RAII容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有标准容器具有与向量< T> 相同的通用API,但通过直接默认构造填充新位置?

Is there a standard container that has the same general API as vector<T> but that populates new locations via direct default construction?

背景:

我有一种类型禁止复制,有一个默认的构造函数,我真正想做的是这样:

I have a type that disallows copying but has a default constructor and what I really want to do is this:

vector<NoCopy> bag(some_size);

// use bag[i]'s

return; // bag & contents get correctly cleaned up. 

但是,这不起作用,因为 vector< T& 按照默认构建对象的方式实现,然后将其复制到每个新位置。

However, this doesn't work because vector<T>(int) is implemented in terms of default constructing an object and then copying it into each of the new locations.

编辑:不是C ++ 0xB(aka C ++ 11)

Not C++0xB (a.k.a. C++11)

推荐答案

一个选择是升级到C ++ 11标准库实现。

One option would be to upgrade to a C++11-compliant Standard Library implementation.

在C ++ 11中,向量(size_type)构造函数default构造N个元素到容器中。它既不复制也不移动任何元素。

In C++11, the vector(size_type) constructor default constructs N elements into the container. It neither copies nor moves any elements.

Visual C ++ 2010不支持此C ++ 11功能;我相信Visual C ++ 11开发者预览正确支持它。我不知道最新版本的libstdc ++是否支持这个;我怀疑libc ++。

Visual C++ 2010 does not support this C++11 feature; I believe the Visual C++ 11 Developer Preview does correctly support it though. I do not know whether recent versions of libstdc++ support this; I would suspect that libc++ does.

这篇关于非可复制类型的整数可索引RAII容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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