- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想创建一个不可复制的分配器(在 C++14 中),它只分配一个 std::vector
可以使用的固定内存块。我想防止分配器(以及 vector )被复制,以防止用户意外分配内存。分配器仅用于 std::vector
或 std::string
。
所以我的分配器有一个这样的复制构造函数:
static_allocator(const static_allocator<T>&) = delete;
调用时:
std::vector<int, static_allocator<int>> vvv(static_allocator<int>(3));
我得到以下编译错误:
/usr/include/c++/5/bits/stl_vector.h: In instantiation of ‘std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_Vector_impl(const _Tp_alloc_type&) [with _Tp = int; _Alloc = static_allocator<int>; std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type = static_allocator<int>]’:
/usr/include/c++/5/bits/stl_vector.h:128:20: required from ‘std::_Vector_base<_Tp, _Alloc>::_Vector_base(const allocator_type&) [with _Tp = int; _Alloc = static_allocator<int>; std::_Vector_base<_Tp, _Alloc>::allocator_type = static_allocator<int>]’
/usr/include/c++/5/bits/stl_vector.h:265:18: required from ‘std::vector<_Tp, _Alloc>::vector(const allocator_type&) [with _Tp = int; _Alloc = static_allocator<int>; std::vector<_Tp, _Alloc>::allocator_type = static_allocator<int>]’
错误似乎是因为在 STL_vector.h:265
中没有定义右值分配器的构造函数:
/**
* @brief Creates a %vector with no elements.
* @param __a An allocator object.
*/
explicit
vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT
: _Base(__a) { }
虽然更深入的代码实际上支持右值分配器,但不会调用这些分配器,因为右值是由上述构造函数通过引用获取的。
这是 C++14 中缺少的功能还是我缺少某些选项?同样奇怪的是,在构造 vector 时无缘无故地复制了分配器。
完整的代码示例可以在这里找到:https://onlinegdb.com/ByqXwQ4k4
最佳答案
根据requirements for an Allocator
type您的分配器类型需要满足 CopyConstructible
这意味着你不能删除你的拷贝:
A a1(a)
A a1 = aCopy-constructs
a1
such thata1 == a
. Does not throw exceptions. (Note: everyAllocator
also satisfiesCopyConstructible
)
关于c++ - 不可复制的 STL 分配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53617084/
我有一个 map我需要插入和删除 Foo * 的地方.用法看起来像 map mapping; while( a long time) { // make ne
我想知道这是做什么的: std::basic_string, std::allocator>:: basic_string, std::allocator> (&myText, "hello worl
是否可以在 C++ 中创建一个像这样简单工作的自定义分配器: { // Limit memory to 1024 KB ScopedMemoryPool memoryPool(1024
我正在使用提到的 STL 分配器 here . 我所做的唯一更改是我从一个名为 Object 的基类继承,并且我使用基类的 new 和 delete 函数进行分配。 class MyAlloc
我有一段代码可以创建数千个对象,并将它们附加到一个 vector 中。下面的代码只是一个正在做的事情的例子,尽管构造函数有一些参数,而for实际上并没有那个条件,但它起到了表明它运行了数千次的目的。
这里有两个问题。首先,如果我需要在 Clone 之前创建 b2BlockAllocator 然后在克隆之后删除(在哪里?)? Xcode 分析工具未显示 C++ 泄漏... b2FixtureDef
我想创建一个不可复制的分配器(在 C++14 中),它只分配一个 std::vector 可以使用的固定内存块。我想防止分配器(以及 vector )被复制,以防止用户意外分配内存。分配器仅用于 st
我在 http://msdn.microsoft.com/en-us/library/ee292117.aspx 上看到和 http://msdn.microsoft.com/en-us/librar
我想用更健壮的分配器替换标准分配器(C++ 标准只需要对 vector::resize 进行溢出检查)。许多库提供的各种 C++ 分配器在进行负面 self 测试时会一败涂地。 我可以使用更强大的分配
我的 STL 容器中的内存使用预计是不稳定的——也就是说它会经常收缩和增长。我正在考虑通过为 STL 容器类型声明指定一个分配器来解决这个问题。我知道矿池分配器旨在处理这种情况,但我担心的是波动性将超
我有一个大量使用 STL 容器和字符串的大型(>250 个文件)库的源代码。我需要在有限堆的嵌入式环境中运行它,所以我想确保这个库本身的堆使用受到限制。 显而易见的解决方案是创建一个分配器,但修改整个
我想知道有一个符合 C++ 标准的库是否可行 allocator使用位于堆栈中的(固定大小的)缓冲区。 不知何故,这个问题似乎还没有在 SO 上这样问过,尽管它可能已经在其他地方得到了隐含的回答。 所
我观察到我的 MSVC10 副本附带的容器似乎允许基于状态的分配器,并编写了一个简单的池分配器,为特定类型分配池。 然而,我发现如果_ITERATOR_DEBUG_LEVEL != 0 MSVC 向量
据我所知,当 vector 空间不足时,分配器用于创建新空间。但是,我想创建一个自定义调整大小策略,该策略将移除底部 25% 的元素并始终保持相同的大小。这是为了构建一个空间有限的缓存。 有没有我可以
我目前正在尝试使用 Microsoft Visual Studio 2012 编译一个相当大的项目。我发现它在旧版本上编译得很好,但是对于这个版本,我在 std::list 的任何地方都会出错仅与一个
因此,在所提供代码的下一行,我有 IntelliSense 警告:“没有可用的成员”。怎么了?在正常情况下,似乎有选项,如“分配”、“解除分配”等。 namespace MyLib { tem
我正在尝试在 Microsoft visual studio 2013 on C++ 上编译为 linux 编写的程序。 声明 sdesc_t *ret = _malloc(sizeof(sdesc_
由于我工作的政策,我无法使用高于 1.33.1 的 Boost 版本,也无法使用高于 4.1.2 的 GCC 版本。是的,这是垃圾,但我对此无能为力。 Boost 1.33.1 不包含进程间库。 也就
我正在为 T 类型的数组实现资源分配克隆操作。直接的实现使用 new T[sz],然后是从源到新数组的 std::copy 调用。它遍历内存两次。 我想分配原始内存然后使用 std::uninitia
我们有一个库,它通过 extern "C" 提供 C 接口(interface),并从 C 代码中使用,但为了方便起见,它内部使用了 STL 容器和一些 C++ 功能,如 RAII。 现在有一个新的要
我是一名优秀的程序员,十分优秀!