草庐IT

MAKE_TEMPLATE

全部标签

c++ - new 和 make_shared 用于共享指针

我遇到了this@kerekSB状态的帖子和答案之一std::shared_ptrp1=std::make_shared("foo");std::shared_ptrp2(newObject("foo"));Inyourcode,thesecondvariableisjustanakedpointer,notasharedpointeratall.Nowonthemeat.make_sharedis(inpractice)moreefficient,becauseitallocatesthereferencecontrolblocktogetherwiththeactualobject

c++ - 使用 Lambda/Template/SFINAE 自动化 try/catch-safeguarding trampoline 函数

我有100个左右的蹦床功能。我想知道是否可以自动将每个包装在try/catchblock中。请提前警告,这不是一个简单的问题。我将首先用(简化的)代码描述问题,然后在下面尝试尽可能地回答它,以便读者可以看到我的位置。Foo有一个函数指针表:编辑:这是一个C函数指针表。所以它可以接受staticW::w。签名在这里:http://svn.python.org/projects/python/trunk/Include/object.h编辑:我尝试了一个测试用例here:classFoo{Tabletable;Foo(){//Eachslothasadefaultlambda.:table

c++ - std::make_pair:无法将 'ch'(类型 'char')转换为类型 'char&&'

这个问题在这里已经有了答案:C++11make_pairwithspecifiedtemplateparametersdoesn'tcompile(1个回答)关闭9年前。以下代码有什么问题:#include#include#include#include#includeintmain(){std::vector>vec;for(unsignedi=0;i(ch,number))!=vec.end());std::cout(ch,number));}}它确实可以很好地编译:g++test.cxx但失败了:$g++-std=c++11test.cxx/tmptest.cxx:Infunct

c++ - boost::make_shared 不是在调用(放置)运算符 new 吗?

我第一次使用boost::make_shared来创建共享指针指向的对象。主要是因为我们的代码太慢了,单次分配确实有助于boost性能。在以“硬手动方式”修复了一些内存泄漏之后,我决定通过覆盖所有相关类的新运算符来实现一个简单的内存泄漏检测器,仅用于计算在我们的应用程序的特定点哪些对象仍然存在。我之前已经实现过几次,惊讶地发现我的代码不再检测到任何对象。我认为我所要做的就是覆盖“placementnew”而不是“normal”operatornew,因为make_shared的boost网站文档中有以下内容:"Effects:Allocatesmemorysuitableforanob

c++ - 初始化 shared_ptr 成员变量,new vs make_shared?

当初始化一个shared_ptr成员变量时://.hclassCustomer{public:Customer();private:std::shared_ptrsomething_;}//.cppCustomer():something_(newOtherClass()){}对比Customer():something_(std::make_shared()){}是否允许使用make_shared版本?我似乎总是看到第一个版本,哪个是首选? 最佳答案 不允许make_shared的唯一时间是:如果您得到一个由其他人分配的裸指针并将

c++ - 为什么此代码会出现错误 "template specialization requires ' template< >'"?

当我尝试用Clang编译它时templatestructField{charconst*name;Field(charconst*name):name(name){}};templateclassCRTP{staticFieldconst_field;};classClass:publicCRTP{};FieldconstCRTP::_field("blah");intmain(){}我明白了error:templatespecializationrequires'template'FieldconstCRTP::_field("blah");~~~~~~~~~~~^我根本不明白这个错

Linux: make/Makefile 相关的知识

背景:会不会写makefile,从一个侧面说明了一个人是否具备完成大型工程的能力一个工程中的源文件不计数,其按类型、功能、模块分别放在若干个目录中,makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于进行更复杂的功能操作makefile带来的好处就是——“自动化编译”,一旦写好,只需要一个make命令,整个工程完全自动编译,极大的提高了软件开发的效率。make是一个命令工具,是一个解释makefile中指令的命令工具,一般来说,大多数的IDE都有这个命令,比如:Delphi的make,VisualC++的nmake,Linux下GNU的

c++ - 模板元编程 : multiplying a bunch of template arguments

我需要在编译时计算传递给模板化结构的一堆数字的乘积。我成功地做了一个丑陋的解决方案:templatestructmul_all{staticconstexprstd::size_tvalue=n1*mul_all;};templatestructmul_all{staticconstexprstd::size_tvalue=1;};问题是每次我必须像这样将0提供给模板参数到我的结构中intmain(){std::cout::value::value;return0;}是否有任何解决方法来读取最后一个零?注意:我是TMP的初学者。 最佳答案

c++ - 为什么要用 make_unique 调用来初始化 unique_ptr?

取自:http://herbsutter.com/2013/05/22/gotw-5-solution-overriding-virtual-functions/为什么要写:autopb=unique_ptr{make_unique()};不仅仅是:autopb=make_unique();我唯一的猜测是,如果我们想要auto,我们需要帮助它推断出正确的类型(这里是base)。如果是这样,那么对我来说这将是一个值得怀疑的优点..键入auto然后在=的右侧键入大量初始化..我错过了什么? 最佳答案 嗯,重点是第一个选项使pb一个uni

c++ - 如何使用 std::make_heap

关闭。这个问题需要更多focused.它目前不接受答案。想改进这个问题吗?更新问题,使其只关注一个问题editingthispost.关闭8年前。Improvethisquestionhttp://www.cplusplus.com/reference/algorithm/push_heap/太乱了。要在std中使用堆,首先将元素放入vector中,然后调用std::make_heap(v.begin(),v.end());如果我向vector中插入元素会怎样?堆会弄乱吗?比如v一开始可能有10个元素,我只做第3个元素到第7个元素的堆,现在我往第5个和第9个位置插入元素,这个过程中堆结