草庐IT

alloc_traits

全部标签

c++ - malloc 和 allocate 有什么区别

我遇到了一些代码,它广泛使用了allocate。例如,char*recordDate=allocate(20)我以前从未使用过allocate,因此问题来了,malloc和allocate之间有什么区别?虽然我不清楚它的优势,但我可以说的一个区别是,malloc提供原始内存,而allocate似乎会提供原始内存,但我不必将指针强制转换为特定类型。 最佳答案 来自allocate文档:Allocatesn*sizeof(T)bytesofuninitializedstoragebycalling::operatornew(std::s

C++ : how do I use type_traits to determine if a class is trivial?

在C++0x中,我想确定一个类是否简单/是否具有标准布局,以便我可以使用memcpy()、memset()等...我应该如何使用type_traits实现下面的代码,这样我才能确认一个类型是微不足道的?templateboolisTrivialType(){boolisTrivial=???returnisTrivial;}注意:is_pod()限制太多:我希望我的类有简单的构造函数等......为了方便。补充:我认为std::is_standard_layout可能会给我我正在寻找的东西。1.如果我添加构造函数,它仍然返回true2.如果我添加一个虚方法,它返回false这是我需要确

c++ - 如何使用 type_traits 检测字符串文字?

我如何可靠地对任何不是字符串文字的内容进行static_assert?例如,在下面的代码中,我试图包装标准断言宏,但静态拒绝消息的任何不是字符串文字的内容(因为当断言触发时,除了字符串文字之外的任何内容都不会在运行时显示).#include#include#include#definemy_assert(test,message)\static_assert(\(\!std::is_pointer::value&&\!std::is_array::value\),\"literalstringrequired"\);\assert((message,(test)));intmain()

c++ std::bad_alloc on std::filesystem::path 追加

我遇到了一个非常奇怪的行为,我将其提炼为一个非常基本的测试:#include#includeintmain(void){conststd::stringname="foo";conststd::filesystem::pathlock_dir="/tmp";std::filesystem::pathlockfile=lock_dir/name;return0;}我用g++-std=c++17-Wall-Wextra-Werror-gfoo.cpp-ofoo编译它。当我运行它时,我在附加两条路径的行上得到一个std::bad_alloc异常。这是我用gdb看到的#0__GI_raise(

c++ - 如何在 C 语言的 gdb 中调试 St9bad_alloc 失败?

我有一个程序失败了:terminatecalledafterthrowinganinstanceof'std::bad_alloc'what():St9bad_alloc我想这与malloc/free有关,但我不知道是哪一个。我可以在gdb中设置什么断点来中断错误,以便我可以查看堆栈跟踪?该程序是C和C++的组合,使用gcc3.4.2编译。 最佳答案 导致异常的并不是真正的malloc/free,它是“新的”,它肯定在您的应用程序的C++部分中。看起来您提供的参数对于"new"分配来说太大了。'std::bad_alloc'是由以下

c++ - std::allocator 构造/销毁与放置 new/p->~T()

对于我的一个项目,我正在从头开始编写一些STL容器(我有我的理由)。由于我非常接近地模仿STL的功能和接口(interface),所以我会尽最大努力遵守“如果它与标准结构同名,它将尽可能符合标准”的政策。所以,当然我的容器将分配器作为模板参数,这非常好,因为它允许一些自定义分配方案。关于我的问题。std::allocator接口(interface)将内存分配与对象构造分开。同样,它将释放与销毁分开。这是有道理的,因为从哪里获取内存与在C++中正确构造对象或多或少无关。所以有两个构造/释放函数,默认实现如下所示(直接从书中摘录):voidconstruct(pointerp,const

c++ - 特化 iterator_traits

我想专攻std::iterator_traits对于不具有通常的嵌套typedef(如value_type、difference_type等)的容器类模板的迭代器,我不应该修改其源代码。基本上我想做这样的事情:templatestructiterator_traits::iterator>{typedefTvalue_type;//etc.};除了这不起作用,因为编译器无法推断出T来自Container::iterator.有什么可行的方法可以达到同样的目的吗?例如:templateclassSomeContainerFromAThirdPartyLib{typedefTValueTy

c++ - 为什么 std::allocator::construct 和 std::allocator::destroy 在元素类型上模板化?

std::allocator的construct和destroy成员函数根据要构造的元素的类型进行参数化:templateclassallocator{public:typedefTvalue_type;typedefT*pointer;templatevoidconstruct(U*p,Args&&...args);templatevoiddestroy(U*p);...};这样做的理由是什么?他们为什么不选择value_type*或pointer?好像allocator应该只知道如何构造或销毁T类型的对象. 最佳答案 这与all

c++ - 错误 C2248 : 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

收到此错误,我很确定它在operatorvoidCRational::print()const{print(cout);}voidCRational::print(ostream&sout)const{if(m_denominator==1)cout 最佳答案 您需要通过引用而不是值返回ostream。它试图调用构造函数。也可以传递'a'作为引用:ostream&operator我还注意到打印方法可能是错误的。它有sout作为流的名称传递,但随后直接使用cout实现。应该是voidCRational::print(ostream&s

c++ - C++11 <type_traits> 模板参数类型推导失败

我正在尝试了解如何使用C++(11).这是我的简单测试程序#includetemplateinlineUadd(typenamestd::enable_if::value,U>::typea,typenamestd::enable_if::value,S>::typeb){returna+b;}intmain(intargc,constchar*argv[],constchar*envp[]){unsignedintui;inti;autoa=add(ui,i);return0;}当使用GCC4.8.1编译时,它会出错/home/per/f.cpp:Infunction‘intmain