草庐IT

move_function_imp

全部标签

c++ - 为什么在取消引用的函数指针上使用时 std::is_function 的计算结果为 false?

我正在尝试使用std::is_function来确定变量是否为函数指针。运行以下代码时#include#includeusingnamespacestd;intmain(){typedefint(*functionpointer)();functionpointerpmain=main;cout::value::value::value::value输出是PFivE0PFivE0FivE1FivE0任何有见识的人都可以解释为什么std::is_function的最后一个表达式的计算结果为false吗?(代码在g++4.7、g++4.8和clang++3.2下测试)

c++ - 为什么 std::function 不能接受推导类型作为其模板参数?

#includeusingnamespacestd;templatevoidf1(CharType*str,functionfn_filter){}templatevoidf2(CharType*str,functionfn_filter){}voidf3(char*str,charc){autofn_filter=[=](chare)->bool{returne==c;};f1(str,fn_filter);//errorC2784f2(str,fn_filter);//OK}intmain(){f3("ok",'k');}//errorC2784:'voidf1(CharType*

c++ - 为什么 std::unary_function 不包含虚拟析构函数

我遇到了类模板std::unary_function和std::binary_function。templatestructunary_function{typedefArgargument_type;typedefResultresult_type;};templatestructbinary_function{typedefArg1first_argument_type;typedefArg2second_argument_type;typedefResultresult_type;};这两个都可以用作特定用途的基类。但是其中仍然没有虚拟析构函数。我猜的原因之一是这些并不意味着要进

C++ Linux : error: ‘move’ is not a member of ‘std’ how to get around it?

所以在我的VS2010上我可以编译如下代码:boost::shared_ptrinternal_thread;boost::packaged_taskinternal_task_w(boost::bind(&thread_pool::internal_run,this,internal_thread));internal_thread=boost::shared_ptr(newboost::thread(std::move(internal_task_w)));前两行在boost1.47.0和linux上没问题...但是在std::move上它给出了error:‘move’isnota

c++ - 确定 std::function 的返回类型

我正在编写一个模板函数,它接收一个std::function对象(通过使用适当的参数调用std::bind生成)。在这个函数中,我想确定这个函数对象的返回类型。有可能吗?事实上,我希望模板函数返回相同的类型。您能想出一种优雅的、基于标准的方法来实现这一目标吗?类似于:templateT::return_typefunctionObjWrapper(TfunctionObject){//...returnfunctionObject();}谢谢 最佳答案 您可以使用decltype和尾随返回类型来完成:templateautofunc

c++ - 将双变量 std::function 转换为单变量

我有一个获取两个值x和y并返回结果的函数:std::functionmult=[](doublex,doubley){returnx*y;};现在我想得到一个常量y的单变量函数。我写了下面的代码,但它不起作用。std::function(std::function,double)>funcYgivenX=[](std::functionfunc2d,doubleinX){return[&func2d,&inX](doubleinY){returnfunc2d(inX,inY);};};我在这里做错了什么?最好(最有效)的方法是什么? 最佳答案

c++ - 为什么需要 move 语义来消除临时拷贝?

所以我对move语义的理解是,它们允许您覆盖用于临时值(右值)的函数,并避免可能昂贵的拷贝(通过将状态从未命名的临时值move到您命名的左值)。我的问题是为什么我们需要特殊的语义?为什么C++98编译器不能省略这些拷贝,因为是编译器决定给定表达式是左值还是右值?例如:voidfunc(conststd::string&s){//Dosomethingwiths}intmain(){func(std::string("abc")+std::string("def"));}即使没有C++11的move语义,编译器仍然应该能够确定传递给func()的表达式是右值,因此不需要从临时对象进行复制

c++ - 与 move 构造函数混淆 : unable to call move constructor

我一直难以理解C++中的move构造函数。我用默认构造函数、复制构造函数、move构造函数和析构函数制作了一个简单的类。此外,我定义了一个具有两个重载的函数,一个接受对该类的引用,一个接受对该类的右值引用。我的测试代码如下。#includeclassc{public:c(){std::cout我得到的输出不是我所期望的。以下是我从此代码获得的输出。defaultconstructorcopyconstructorpassedbyreferencedefaultconstructorpassedbyrvaluereferencedestructor除了第3行,我能理解所有行的输出。在第3

c++ - std::move 的类型是什么?

此代码按预期工作(在线here)。最后v是空的w不是空的,因为它窃取了v的内容.vectorv;v.push_back(1);coutw(vp);cout但是如果我替换autovp=move(v)与vector&&vp=move(v);然后它就不动了。相反,它复制并且两个vector最后都是非空的。如图here.说明:更具体地说,vp的自动派生类型是什么??如果不是vector&&,那还能是什么呢?尽管这两个示例如此相似,但为什么会给出不同的结果?Extra:这个我也试过了,还是复制而不是movestd::remove_reference>::type&&vp=move(v);

c++ - "error: no matching function for call to"

我当时在键盘上,我正在尝试使用C++来提高我的技能。我以前从未使用过模板,所以我尝试研究如何使用它们。下面的代码是结果,不幸的是,它不起作用。我确实尝试寻找问题的解决方案,但由于我没有太多使用模板的经验,所以我无法在我的问题和其他问题之间建立任何联系。所以,我决定寻求帮助。templateclassVector2{public:Ax,y;Vector2(Axp,Ayp){this->x=xp;this->y=yp;}};templateclassrayToCast{public:rayToCast(Bangle,Vector2origin,Vector2point1,Vector2po