草庐IT

static_allocator

全部标签

c++ - 为什么 allocate_shared 和 make_shared 这么慢

我刚刚编写了一个测试程序来找到分配和释放许多由shared_ptr管理的对象的最快方法。我尝试了shared_ptr和new,shared_ptr和pool,make_shared,allocate_shared。让我惊讶的是allocate_shared比shared_ptr和pool慢。我使用发布版本测试vs2017+win10中的代码。发布build设置为默认(/O2)。我还在gcc4.8.5+centos6.2中使用g++-std=c++11-O3对其进行了测试。代码是:#include#include#include#include#include#includeusingn

c++ - Boost 池分配器不会在 g++ 中使用 std::allocate_shared 进行编译

编辑:澄清我想要的结果,因为我没有很好地传达它:能够将std::allocate_shared与boost::fast_pool_allocator一起用作使用g++4.8或更高版本和boost1.56.0的分配方法。目前这适用于g++4.6,但在4.7、4.8和4.9上失败。需要说明的是,我不希望在g++4.7中使用这项功能。测试代码产生错误:#include"boost/pool/pool.hpp"#include"boost/pool/pool_alloc.hpp"#includeintmain(intargc,char**argv){autofails=std::allocat

c++ - 隐式内部链接与显式内部链接 ("static"不同)?

今天我遇到了一个特殊性,虽然可能不是很重要,但仍然让我感到困惑。也许我也没有正确理解C++。源文件中的一些数组指向字符串文字,如下所示:constchar*a[]={"a","b","c"};constchar*b[]={"d","e"};constchar*c[]={"f","g"};除了传递给GetProcAddress以从库中检索函数指针外,这些指针数组都没有以任何方式使用(这是一个非阻塞动态OpenAL/EFX/捕获函数加载器和上下文创建者/管理者)。我最终想到,我可能应该将这些变量声明为staticconst,因为在那个.cpp文件之外的任何地方都不需要它们,因此明确内部链接

c++ - 借助 static_assert 改进诊断

在模板编程中,static_assert帮助程序员检查模板参数的约束并在违反约束时生成人类可读错误消息。考虑这段代码,templatevoidf(T){static_assert(T(),"firstrequirementfailedtomeet.");static_assert(T::value,"secondrequirementfailedtomeet.");Tt=10;//eventhismaygenerateerror!}我的想法是:如果第一个static_assert失败,这意味着一些T的要求不满足,因此编译应该停止,只生成第一个错误消息——因为继续编译只是为了生成越来越多

c++ - static constexpr 指向函数的指针,编译器之间的区别

回答thisquestion时,我用gcc(codecompiled)和clang(coderejected)尝试了以下代码:typedeflong(*func)(int);longfunction(int){return42;}structTest{staticconstexprfuncf=&function;};templatestructCall{staticvoidf(){c(0);}};intmain(){Call::f();}我不确定哪个编译器是正确的,虽然我认为Test::f的constexpr初始化是可以的。错误clang输出是:error:non-typetempla

c++ - static_cast 的指针值

在当前标准草案(和C++17)中,this是关于static_castingvoid*的:Aprvalueoftype“pointertocv1void”canbeconvertedtoaprvalueoftype“pointertocv2T”,whereTisanobjecttypeandcv2isthesamecv-qualificationas,orgreatercv-qualificationthan,cv1.IftheoriginalpointervaluerepresentstheaddressAofabyteinmemoryandAdoesnotsatisfytheali

c++ - 什么是 "allocation context"?

我是一名学生,我必须进行有关内存泄漏检测的研究。在许多论文中,他们都在谈论分配背景。我不知道这是什么意思。我找不到任何allocationcontext的定义(或翻译,我来自德国)。举个例子,引用一篇论文(DetectingMemoryLeaksthroughIntrospectiveDynamic使用机器学习的行为建模):Thekeyideabehindusingmachinelearningisthataleakingobjectisdiscerniblebyobservingthelifetimesofothersimilarobjects.Thatis,anobjectcanb

c++ - 用于确保设计契约(Contract)的 static_assert

作为开发人员团队的一员,我想确保在我们发布的自定义迭代器上实现一组函数(和运算符)。使用STL迭代器类型作为基类型会有所帮助,但是由于某些原因(超出我的控制范围),我们决定不强制执行STL兼容性。迭代器由同一个团队和整个公司的人员使用。我想设计一个使用迭代器类型并根据设计契约进行测试的模板类。例如,我希望迭代器实现operator++、operator--并声明所需的typedef。1>是否可以实现这样一个强制设计契约的模板类?可能使用static_assert?2>如果是,这是一个好的设计吗?引用:customiterator 最佳答案

c++ - reinterpret_cast 与 static_cast 用于在标准布局类型中写入字节?

我需要写入某些整数类型的单个字节。我应该使用reinterpret_cast,还是应该通过void*使用static_cast?(一)unsignedshortv16;char*p=static_cast(static_cast(&v16));p[1]=...somecharvaluep[0]=...somecharvalue或(b)unsignedshortv16;char*p=reinterpret_cast(&v16);p[1]=...somecharvaluep[0]=...somecharvalue根据static_castandreinterpret_castforstd:

c++ - const static auto lambda 与引用捕获一起使用

在C++11函数中使用一些本地lambda对象时,我很想将它们声明为conststaticautolambda=...只是为了让编译器知道只有一个std::function需要对象(并可能优化调用和/或内联它),但我意识到在这种情况下通过引用捕获局部值会导致奇怪的行为。考虑以下代码:voidprocess(constData&data,conststd::function&lambda){...}voidSomeClass::doSomething(){intfoo=0;conststaticautolambda=[&foo](){....++foo;....}process(data