- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在下面的代码中是函数 make_vector()。它创建一个 vector 并将其返回给调用者。我希望能够为要使用的 vector 指定一个分配器,但默认情况下使用默认的 std::allocator。这是因为在某些情况下,默认分配器就是我所需要的,但其他时候我需要从一些预定义的内存池中进行分配。
我最接近的是 make_vector2() 函数模板。它适用于 std::allocator,但我不知道如何将“arena”参数传递到我的自定义分配器中。
希望这个工作的 c++11 示例能更好地解释它:
#include <malloc.h>
#include <cinttypes>
#include <cstddef>
#include <iostream>
#include <limits>
#include <stdexcept>
#include <vector>
namespace mem
{
// Memory arena to allocate from.
enum class ARENA
{
TEXTURES,
FONTS,
SCRIPTS
};
// Allocate block from specific arena.
void *malloc( const std::size_t size, const ARENA arena )
{
return std::malloc( size /*, arena */ );
}
// Free block from specific arena.
void free( void *ptr, const ARENA arena )
{
std::free( ptr /*, arena */ );
}
// The allocator - forward declaration.
// Not derived from std::allocator - should it be?
// Based on code from here:
// http://drdobbs.com/184403759?pgno=2
template<typename T> class allocator;
// Specialised for void.
template<> class allocator<void>
{
public:
typedef std::size_t size_type;
typedef ptrdiff_t difference_type;
typedef void* pointer;
typedef const void* const_pointer;
typedef void value_type;
template<typename U> struct rebind
{
typedef allocator<U> other;
};
};
template<typename T> class allocator
{
public:
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
template<typename U> struct rebind
{
typedef allocator<U> other;
};
allocator( ARENA arena ) noexcept :
arena_( arena )
{}
~allocator() noexcept
{}
pointer address( reference x ) const
{
return &x;
}
const_pointer address( const_reference x ) const
{
return &x;
}
pointer allocate( size_type n, allocator<void>::const_pointer hint = 0 )
{
void *p = mem::malloc( n * sizeof( T ), arena_ );
if ( p == nullptr )
{
throw std::bad_alloc();
}
return static_cast<pointer>( p );
}
void deallocate( pointer p, size_type n )
{
mem::free( p, arena_ );
}
size_type max_size() const noexcept
{
return std::numeric_limits<std::size_t>::max() / sizeof( T );
}
void construct( pointer p, const T& val )
{
new (p) T(val);
}
void destroy( pointer p )
{
p->~T();
}
allocator( const allocator& src ) noexcept
{
arena_ = src.arena_;
}
ARENA arena_;
};
} // namespace mem
template<class T1, class T2> bool operator==( const mem::allocator<T1> &alloc1, const mem::allocator<T2> &alloc2 ) noexcept
{
return alloc1.arena_ == alloc2.arena_;
}
template<class T1, class T2> bool operator!=( const mem::allocator<T1> &alloc1, const mem::allocator<T2> &alloc2 ) noexcept
{
if alloc1.arena_ != alloc2.arena_;
}
// How do I allow the custom allocator to be passed? Function parameter? Template?
std::vector<uint8_t> make_vector()
{
std::vector<uint8_t> vec;
// Do stuff with the vector
return vec;
}
// This template function seems to work with std::allocator
template< typename T > std::vector<uint8_t,T> make_vector2()
{
std::vector<uint8_t,T> vec;
// Do stuff with the vector.
return vec;
}
int main( int argc, char **argv )
{
// vec1 - Allocates from TEXTURES arena
// See the C++11 FAQ by Bjarne Stroustrup here:
// http://www2.research.att.com/~bs/C++0xFAQ.html#scoped-allocator
std::vector<uint8_t, mem::allocator<uint8_t>> vec1( mem::allocator<uint8_t>{mem::ARENA::TEXTURES} );
// vec2 - Make the vector using the default allocator.
auto vec2 = make_vector2< std::allocator<uint8_t> >();
return 0;
}
在 main() 中创建 vec1 以使用 TEXTURES arena 进行分配。要使用的竞技场被传递到分配器的构造函数中。 Vec2 由 make_vector2() 模板函数创建并使用 std::allocator。
问:我如何定义 make_vector() 函数,以便它可以创建一个使用上面的 std::allocator 或自定义池分配器的 vector ?
最佳答案
在 C++11 中,函数模板可以有默认的模板参数:
template<class T, class Alloc = std::allocator<T>>
std::vector<T, Alloc> make_vector(Alloc const& al = Alloc()){
std::vector<T, Alloc> v(al);
// ...
return v;
}
在C++03(或不支持该特性的编译器)中,它有点麻烦,但你可以基于模板参数重载:
template<class T>
std::vector<T> make_vector(){
std::vector<T> v;
// ...
return v;
}
template<class T, class Alloc>
std::vector<T, Alloc> make_vector(Alloc const& al = Alloc()){
std::vector<T, Alloc> v(al);
// ...
return v;
}
关于c++ - 如何创建一个函数,返回一个 vector ,它使用自定义池分配器(如果提供),但默认为 std::allocator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9410826/
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
class Person def name puts "Doharey" end end puts Person.class #=> this out puts Class puts
在 PHP 中,($myvariable==0)当 $myvariable 为零时,表达式的值为真;当 $myvariable 为 null 时,此表达式的值也为 true。如何排除第二种情况?我的意
正文 Oracle的一顿猛如虎操作,让开发者彻底失去了Java EE。Eclipse基金会则自立门户,另起炉灶开启Jakarta EE项目。 对于Jakarta EE,从它
我是 python 新手,建议我使用 Canopy。我正在努力跟进 with this tutorial ,但我陷入了 mahotas.imread 行。我收到一个错误,说以这个结尾: Full er
上下文是我们想要跟踪应用程序的用户行为,因为它具有不同的功能。 为此,我们创建了一个自定义 Angular Directive(指令),例如myFunctionality并将 HTML 部分包装到此指
我正在尝试在文本字段中实现 google Places api 的自动完成功能。这是我的代码: $(document).ready(function() { initialize(){ v
我在 Glassfish 3.1.1 中配置了一个新的 jdbcRealm 并打开了 FINEST 日志记录,当我尝试使用用户名和密码登录时,我得到以下信息。它提示我的 Web 应用程序映射到的领域是
问题是,即使我将线程设置为“thrd.IsBackground = false”,iis 也不认为它正在运行,即使这是一个长时间运行的进程。如果我不关闭应用程序池的空闲关闭,它将关闭,因为它认为它是空
我正在使用 OpenJDK 8(从 https://jdk.java.net/java-se-ri/8 下载并解压,添加到 PATH),并且遇到了证书错误。 经过调查,我意识到 cacerts 存在问
我基于 Firebase 制作了简单的后期制作项目。我将帖子保存到 Firebase 中,如下所示: let data = UIImageJPEGRepresentation(newPostImage
我觉得还是先说明情况比较好。 情况 我正在编写一些软件来过滤 Set 的 File。 过滤器如下:如果文件未隐藏,则将其添加到新的 Set。 问题在于 File.isHidden() 的当前行为如下:
我创建了一个 C++ DLL 函数,它使用多个数组来处理最终的图像数据。我正在尝试通过引用传递这些数组,进行计算,然后通过预分配数组中的引用将输出传回。在该函数中,我使用了 Intel Perform
我在 python 中有一个小应用程序,除了这个小问题之外,它工作得很好:它应该连续运行一个循环,直到用户通过按钮告诉它停止,但是当我点击开始按钮时,Windows 告诉我它不是回应。现在,如果我编写
代码运行正常,但我怎么会得到这个错误日志 错误日志: 08-28 08:44:24.281: E/MediaPlayer(32454): mOnVideoSizeChangedListener is
我有一个使用 Karma+Jasmine 和 JSHint 的 Grunt 设置。每当我在我的规范文件上运行 JSHint 时,我都会收到一系列“未定义”错误,其中大部分是针对 Jasmine 的内置
将以下代码保存到文件中,Ubuntu 14.04 正确地意识到它是 bash: #!/usr/bin/env bash awk '{print $1 $2}' my_file 然而,向 awk 添加关
以下代码返回 false import inspect print(inspect.isbuiltin(map)) 但是 map 功能在"built-in" functions下列出. 为什么会这样?
这是一段常见的示例代码: while (1) { print "foo\n"; } 永远打印“foo”。 perl foo.pl foo foo foo ... 和 while (0) { p
我对 Haskell 比较陌生,来自 F#(一种 Microsoft 语言)。 我已经从脚手架创建了一个 Yesod 项目,稍微玩了一下,调整了一些东西,但随后它停止工作,并显示此错误消息(在所有模块
我是一名优秀的程序员,十分优秀!