jjzjj

allocator

全部标签

c++ - std::vector<int, std::allocator<char>> 有效吗?

标准没有说明std::vector的分配器但只需要分配器满足Allocator概念。没有关于分配器的value_type,没有reference_type,什么都没有。我以为std::vector内部重新绑定(bind)A到T的分配器,所以我给了一个vectorstd::allocator它按预期工作。但是,如果std::allocator,GCC会生成错误给出,如下:/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.2/include/g++-v4/ext/alloc_traits.h:Ininstantiationof‘struct__gnu_cxx::__

c++ - std::allocator_traits 默认为具有多个模板参数的分配器

std::allocator_traits当我提供一个带有单个模板参数的分配器的STL样式容器时,它会自动发挥它的魔力,但当我提供一个带有两个模板参数但其他方面相似的分配器的STL样式容器时,它不会自动发挥作用。我需要做什么来告诉std::allocator_traits如何与具有多个模板参数的分配器交互?是否有可能获得std::allocator_traits在这种情况下提供合理的默认值?例如,如果我采用HowardHinnant在AllocatorBoilerplate中提供的简单分配器并将其提供给std::vector那么一切都很好。如果我添加一个虚拟intallocator的参

C++ Stack-allocated 对象赋值和析构函数调用

我试图理解在为堆栈上分配的对象分配新值时出现的奇怪行为(对于同一数据集,析构函数被调用两次)。我将从代码片段及其输出开始:classFoo{public:Foo(conststring&name):m_name(name){log("constructor");}~Foo(){log("destructor");}voidhello(){log("hello");}private:stringm_name;voidlog(conststring&msg){cout输出:Foo.0x7fff58c66a58[f1]constructorFoo.0x7fff58c66a58[f1]hell

c++ - "All memory allocated on the stack is known at compile time"是什么意思?

阅读thisgreattutorial关于堆栈与堆,我对这句话有疑问:在堆栈上分配的所有内存在编译时都是已知的。我的意思是,如果我处于取决于用户输入的for循环中(i从0到X),并且在for我在堆栈上分配内存(例如创建一些类的新实例并放入类容器中),它不知道编译程序时堆栈将如何增长(它错过了用户的输入)。我是不是误会了什么? 最佳答案 对读者来说,所做的陈述稍微简化了一点。你是对的,堆栈本质上是动态的,实际分配的数量可能因动态输入而异。这是一个带有递归函数的简单示例:voidf(intn){intx=n*10;if(x==0)ret

c++ - 分配器 : how are the standard containers expected to work internally?

作为这个问题的示例,我将使用std::vector。它的定义来自documentation如下:template>class vector;正如预期的那样,如果T是它的类型,分配器应该偏向于T。总之,下面的代码编译没有错误(至少,使用GCC)并运行:#include#include#includestructS{inti;doubled;std::strings;};intmain(){std::allocatoralloc;std::vector>v{alloc};v.push_back(S{});}在这里,我通过使用专注于int的分配器创建vector的S。它是合法的代码吗?我应该

c++ - 分配器感知容器分配是如何实现的?

例如,来自std::deque::operator=inC++Reference:(1)复制赋值(conststd::deque&other)Replacesthecontentswithacopyofthecontentsofother.Ifstd::allocator_traits::propagate_on_container_copy_assignment()istrue,thetargetallocatorisreplacedbyacopyofthesourceallocator.Ifthetargetandthesourceallocatorsdonotcompareequ

c++ - 未在部分特化中使用的模板参数

我有以下代码:template>classCarray{//...typedefT*pointer;typedefpointeriterator;//...};现在我正在尝试对iterator_traits进行部分特化。对我来说似乎没问题,但g++4.4.5提示:#includenamespacestd{templatestructiterator_traits::iterator>{//line128typedefTvalue_type;typedeftypenameAllocator::difference_typedifference_type;typedeftypenameAl

c++ - 在 shared_ptr 中使用 deallocator & allocator

我正在使用一些库函数,这些函数返回使用malloc或new创建的指针。因此,我根据使用的分配类型有自己的客户解除分配器。例如shared_ptrptr1(LibFunctA(),&MallocDeleter);//LibFunctAreturnspointercreatedusingmallocshared_ptrptr2(LibFunctB(),&newDeleter);//LibFunctBreturnspointercreatedusingnew现在,我知道这是对上述deallocator的一种非常幼稚的使用,但它还大量用于哪些其他场景?此外,如何使用客户分配器?我尝试如下分配自

c++ - 用户定义类型的 vector

尝试编译这段代码时:#include#includeusingnamespacestd;classTest{inta;public:Test(intpa):a(pa){}voidprint(){std::coutvet;vet.push_back(&t);return0;}gcc4.4.5-8报告各种错误,开始于:Infileincludedfrom/usr/include/c++/4.4/i486-linux-gnu/bits/c++allocator.h:34,from/usr/include/c++/4.4/bits/allocator.h:48,from/usr/include

c++ - 如何解析像 std::allocator_traits 这样的可选嵌套类型?

分配器可以选择嵌套类型,如pointer,const_pointer.但是可以始终将这些接口(interface)与std::allocator_traits一起使用,如果这些类型在Allocator中不存在,它将提供这些类型的默认版本.如何std::allocator_traits实现的?模板如何在不存在时选择嵌套类型的默认版本? 最佳答案 解决方法是引用类型T::pointer在不是有效类型时不会导致错误的情况下,它会导致模板参数推导失败。其一般形式称为SFINAE,代表“替换失败不是错误”。有关其工作原理的解释,请参阅我的SF