草庐IT

c++ - 为什么 std::map emplace 需要 gcc 上的复制构造函数?

以下代码在VisualStudio的VisualC++19.0编译器上编译良好,但gcc5.4.0提示复制构造函数是私有(private)的。#includeclassCMyClass{public:CMyClass(int){};private:CMyClass(constCMyClass&);//Iwanttoavoidcopyconstruction};intmain(){std::mapmymap;mymap.emplace(0,0);return0;}错误信息:‘CMyClass::CMyClass(constCMyClass&)’isprivate避免复制不正是emplac

c++ - emplace_back 和 VC++ 的挫败感

我正在使用VisualStudio2012,同时使用默认编译器和NovCTP编译器进行尝试,下面显示了我的问题:structdoesCompile{intmA,mB,mC,mD,mE;doesCompile(inta,intb,intc,intd,inte):mA(a),mB(b),mC(c),mD(d),mE(e){}};structdoesNotCompile{intmA,mB,mC,mD,mE,mF;doesNotCompile(inta,intb,intc,intd,inte,intf):mA(a),mB(b),mC(c),mD(d),mE(e),mF(f){}};int_tm

c++ - emplace 中的危险隐式转换

以下代码使用gcc6.3(https://godbolt.org/g/sVZ8OH)编译时没有任何错误/警告,但由于下面标记的无效内存访问,它包含危险的未定义行为。根本原因是在emplace_back中执行的隐式转换。谁能提出避免此类代码错误的好方法或最佳做法?#include#includestructFoo{explicitFoo(constint&i):i{i}{}voidfoo()const{std::coutfv;fv.emplace_back(d);} 最佳答案 如果您要接受一个const引用,但您不想要一个临时引用,请

c++ - GCC 和 VC++ 之间 std::vector::emplace_back 的区别

这个问题在这里已经有了答案:Canstd::vectoremplace_backcopyconstructfromanelementofthevectoritself?(3个答案)关闭7年前。我听说ModernC++的建议之一是使用emplace_back而不是push_back来追加到容器中(emplace_back接受容器中存储类型的任何构造函数的任何版本参数。根据标准草案N379723.3.6.5(1),说:Remarks:Causesreallocationifthenewsizeisgreaterthantheoldcapacity.Ifnoreallocationhappe

c++ - std::make_unique(以及 emplace、emplace_back)对 initializer_list 参数的笨拙推导

假设我有这个结构:structposition{intx,y;};和另一个将this作为构造函数参数的类:classpositioned{public:positioned(positionp):pos(p){}private:positionpos;};我怎样才能得到简单的autobla=std::make_unique({1,2});上类?目前,编译器试图通过initializer_list匹配并调用make_unique的数组变体,这很愚蠢,因为positioned只有一个构造函数。emplace出现同样的问题和emplace_back功能。几乎所有将其可变模板参数转发给类的构造

c++ - 为什么 emplace_back ("Hello") 调用 strlen?

Justin'sanswer在另一个问题上做了一个我觉得很有趣但无法解释的观察。考虑以下代码:std::vectorv;v.push_back("Hello,world!");//Doesn'tcallstrlen.v.emplace_back("Hello,world!");//Callsstrlen.如果您查看程序集,emplace_backgeneratesacalltostrlen,而push_backdoesnot(使用-Ofast在gcc8.1和clang6.0中测试)。为什么会这样?为什么不能emplace_back优化strlen在这里打电话?我最初的想法是push_b

c++ - 元组到元组的 std::map 和使用 emplace

考虑以下代码,使用g++7.0.1(-std=c++17)编译:#include#includeintmain(){//CreateanaliasforatupleofthreeintsusingThreeTuple=std::tuple;//Createanaliasforamapoftupletotuple(ofthreeints)usingMapThreeTupleToThreeTuple=std::map;MapThreeTupleToThreeTuplem;//ThefollowingdoesNOTcompilem.emplace({1,2,3},{4,5,6});//...

c++ - vector::emplace_back 与 shared_ptr 的用法

#include#include#includeusingnamespacestd;structBinaryTree{intelement;shared_ptrleft;shared_ptrright;};intmain(){vector>vecBT;//caseIvecBT.emplace_back(newBinaryTree{10,nullptr,nullptr});//caseIIvecBT.emplace_back(shared_ptr(newBinaryTree{20,nullptr,nullptr}));return0;}http://en.cppreference.com

c++ - vector.emplace_back() 和 vector.push_back() 做同样的事情吗?

所以我试图将整数添加到我的vector的背面,并错误地认为push_back()将新数据添加到vector的前面(又名vector[0])。我在Xcode中做了一个测试,并针对emplace_back()测试了push_back()并得到了相同的结果。我以为他们是不同的,但这让我觉得也许他们做同样的事情。如果是这样,为什么vector有不同的方法?这是我的代码,以防我这样做:#include#includeusingnamespacestd;intmain(intargc,constchar*argv[]){//forpush_backvectorpush;push.push_back

c++ - map::emplace 在什么时候创建对象?

是std::map::emplace的点创建以某种方式在标准中指定的对象(即调用构造函数)?如果是,它是在检查此类key的存在之前发生的还是之后发生的?在以下情况下很重要:structX{};std::map>map;voidf(intx){map.emplace(x,newX);}如果object是先创建的,一切都很好(unique_ptr是构造出来的并拥有资源),但是如果是在check之后构造的,在重复key的情况下会出现内存泄漏。我在Standard中所能找到的就是Insertsavalue_typeobjecttconstructedwithstd::forward(args)