草庐IT

move_function_imp

全部标签

c++ - gcc -finline-functions 行为?

我将gcc与-finline-functions优化一起用于发布构建。为了对抗代码膨胀,因为我在嵌入式系统上工作,我想说不要内联特定功能。显而易见的方法是通过函数属性,即attribute(noinline)。问题是当我打开作为-O3开关一部分的全局-finline-functions优化时,这似乎不起作用。它也与它被模板化有关,因为同一函数的非模板化版本没有像预期的那样被内联。有人知道当这个全局开关打开时如何控制内联吗?代码如下:#include#includeusingnamespacestd;classBase{public:templatestatic_Type_fooT(_T

c# - "C# base class virtual function"- "override in Managed C++ ref class"

我有一个.NET_4ManagedC++ref类,它派生自用C#编写的.NET_4基类。C#基类:namespaceCore{publicclassResourceManager{publicclass_Resource{publicvirtualvoidDelete(){}}}}托管C++类:namespaceInput.DI{publicrefclassMouse:ResourceManager::_Resource{public:virtualvoidDelete(){}};}这是我遇到的错误:'Input::DI::Mouse::Delete':matchesbaserefcl

c++ - std::move 内部 move 赋值运算符

我读入了anotherquestion在实现move构造函数时,最好对初始化列表中的每个成员进行std::move,因为如果该成员恰好是另一个对象,那么将调用该对象的move构造函数。像这样...//MoveconstructorCar::Car(Car&&obj):prBufferLength(std::move(obj.prBufferLength)),prBuffer(std::move(obj.prBuffer)){obj.prBuffer=nullptr;obj.prBufferLength=0;}然而,在我见过的所有示例move赋值运算符中,都没有提到使用std::move

c++ - 为什么 vector 被认为是 nothrow_move_constructible?

vector的移动构造函数的规范是(从标准中复制出来的):vector(vector&&);注意缺少noexcept.但是gcc4.8和Clang3.2都报告了std::is_nothrow_move_constructible>::value返回真(即1):#include#includeintmain(){std::cout>::value造成这种明显差异的原因是什么? 最佳答案 标准允许实现根据方法加强异常规范17.6.5.12Restrictionsonexceptionhandling[res.on.exception.h

c++ - 编译器是否可以在仅引用一次时隐式 std::move 参数?

假设我有一个带有setter的简单类:classMyClass{public:voidsetName(std::stringname){_name=std::move(name);}private:std::string_name;};我在这里使用std::move,但是如果我忽略它而只写_name=name,编译器是否可以隐式movename参数,因为它没有在setter的其他任何地方使用?它几乎可以被视为赋值表达式中的右值,因为它在其他任何地方都没有被名称引用。编译器能做到吗?现有的编译器会这样做吗? 最佳答案 在什么之上添加N

c++ - std::function 到对象的成员函数和对象的生命周期

如果我有一个std::function的实例,它绑定(bind)到一个对象实例的成员函数,并且该对象实例超出范围,否则将被销毁,我的std::function对象现在被认为是一个坏指针,调用时会失败?例子:intmain(intargc,constchar*argv){type*instance=newtype();std::functionfunc=std::bind(type::func,instance);deleteinstance;func(0);//isthisaninvalidcall}标准中是否有规定应该发生什么?我的直觉是它会抛出异常,因为对象不再存在编辑:该标准是否

c++ - 是否可以从 std::string 中获取内存(就像 string move ctor 那样)?

如果我的内部类是我自己的vector版本(我控制来源)并且为了举例,我不能将其更改为std::string有没有办法从std::string窃取内存,就像std::string的move构造函数一样做。所以像这样:std::stringstr{"abcdefghijklmnopqrstu"};MyVectorCharClassmvc(std::move(str));//Constructortakesmemoryfromstr我想我听说过一些future的建议,以添加.release()至std::string或std::vector但我说的是现在。 最佳答

c++ - 在 std::function 中返回初始值设定项列表而不是 vector

Edit:Itisnotduplicatedofthelinkedquestion(whichisminealso).Hereallthereturntypesarestd::vector.Idonotwanttoreturnaninitializer-list.Iwanttofillthereturnedstd::vectorbyinitializer-listdirectly让我们以这四种情况为例:1)//Acceptablestd::vectorfoo(){returnstd::vector{1};}2)//Acceptablestd::vectorfoo(){return{1}

c++ - 防止 gcc 中的 std::function 分配内存或增加阈值

有什么方法可以防止gcc中的std::function为较大的函数对象动态分配内存?我希望下面的代码可以在没有动态分配的情况下工作:#include#include//replaceoperatornewanddeletetologallocationsvoid*operatornew(std::size_tn){std::cout&stuff){returnstuff();}};intmain(){TestPlatetestor;testor.setValue(15);conststd::function&func=std::bind(&TestPlate::getValue,&te

c++ - 从类中返回可 move 的成员变量

classfoo{public:barsteal_the_moveable_object();private:barmoveable_object;};main(){foof;automoved_object=f.steal_the_moveable_object();}如何实现steal_the_movebale_object将moveable_objectmove到moved_object中? 最佳答案 您可以直接在返回语句中简单地move成员:classfoo{public:barsteal_the_moveable_obje