草庐IT

unary_function

全部标签

c++ - 线程错误 : invalid use of non-static member function

这个问题在这里已经有了答案:Passingmemberfunctionstostd::thread[duplicate](2个答案)关闭5年前。我想了解C++中的线程,但我不知道如何解决这个问题。我想调用两个线程来运行名为“createS”的函数,但出现此错误:error:invaliduseofnon-staticmemberfunction我读过关于这个主题的其他问题,但我真的不明白如何让我的代码工作。有人可以向我解释我做错了什么并尝试帮助我找到解决方案吗?test_class.cppvoidtest_class::generateS(){map1=newmultimap>;map

c++ - std::function 有性能问题,如何避免?

我有一些类允许复合协方差函数(也称为内核,请参阅https://stats.stackexchange.com/questions/228552/covariance-functions-or-kernels-what-exactly-are-they),然后在给定新内核的情况下计算协方差,例如:autoC=GaussianKernel(50,60)+GaussianKernel(100,200);autoresult=C.covarianceFunction(30.0,40.0);但问题是当我想计算协方差时我调用了一个std::function,有没有简单的方法可以避免它?请注意,我

c++ - C++11之前有类似std::function的东西吗?

应该使用什么构造来代替std::function当C++11不可用时?替代方案基本上应该允许从另一个类访问一个类的私有(private)成员函数,如下例所示(不使用std::function的其他功能)。Foo类是固定的,不能改变太多,我只能访问Bar类。classFoo{friendclassBar;//addedbyme,restoftheclassisfixedprivate:voiddoStuffFooA(inti);voiddoStuffFooB(inti);};classBar{public:Bar(Foofoo,std::functionfunc){myFoo=foo;m

c++ - "error: ’ myfn' declared as function returning a function"是什么意思?

我正在尝试编写一个返回函数指针的函数。这是我的最小示例:void(*myfn)(int)()//Doesn'twork:supposedtobeafunctioncalledmyfn{//thatreturnsapointertoafunctionreturningvoid}//andtakinganintargument.当我用g++myfn.cpp编译它时,它打印出这个错误:myfn.cpp:1:19:error:‘myfn’declaredasfunctionreturningafunctionmyfn.cpp:1:19:warning:extendedinitializerli

我的类中的 c++ condition_variable wait_for 谓词,std::thread <unresolved overloaded function type> error

我想在我的类中使用一个线程,然后该线程需要使用一个condition_variable,条件变量将被阻塞,直到一个谓词被更改为true。代码如下所示:classmyThreadClass{boolbFlag;threadt;mutexmtx;condition_variablecv;boolmyPredicate(){returnbFlag;}intmyThreadFunction(intarg){while(true){unique_locklck(mtx);if(cv.wait_for(lck,std::chrono::milliseconds(3000),myPredicate)

c++ - std::function 是否支持完美转发?

std::function是否支持参数的完美转发?我决定测试一下:structWidget{voidoperator()(intconst&){std::coutf{Widget()};intx=5;f(x);猜猜调用了哪个operator()-那个采用右值引用的。看来这是设计使然。这种行为背后的基本原理是什么? 最佳答案 是也不是。是的,论点被转发了。但是不,重载决议不是根据您在调用时提供的参数完成的-它是根据std::function的模板参数完成的。.std::function意味着你有一个需要int的可调用对象,这隐含地是一

c++ - "if constexpr"与 "try in constexpr function"警告交互

我声称thisprogram应该是合式的:它声明了S的constexpr成员函数.但是,GCC和Clang都拒绝这个程序。templatestructS{constexprintfoo(){ifconstexpr(std::is_same_v){return0;}else{try{}catch(...){}return1;}}};intmain(){Ss;returns.foo();//expect"return0"}海湾合作委员会说:error:'try'in'constexpr'functionclang说:error:statementnotallowedinconstexprf

c++ - std::function 的移动构造函数的异常规范是什么?

我看过cppreference.com并且它们似乎表明在std::function(std::function&&)上没有noexcept规范。这对我来说似乎很奇怪。在这种情况下,标准真的不提供不抛出保证吗? 最佳答案 引用标准(根据您的要求):C++11§20.8.11.2.1/6(fromN3290):function(function&&f);templatefunction(allocator_arg_t,constA&a,function&&f);Effects:If!f,*thishasnotarget;otherwis

c++ - 如何将具有不同参数的 std::function 传递给同一函数

我希望将三个功能合并在一起。每个都将std::function作为第一个参数,然后在try/catchblock中执行它。问题是,存在三种不同类型的函数。不带参数的函数、带一个整数参数的函数和带两个整数参数的函数。整型参数的也有相应的参数通过原函数传递。如您所见,每个功能几乎相同,所以如果我能将它们合并在一起就好了。但是,我不确定是否要设置一个可以接收任何形式的std::function的参数,并且还依赖于它已经提供了相应的数据以供使用。函数如下:voidrun_callback(std::function&func){try{func();}catch(conststd::excep

c++ - 比较两个 boost::function

voidff(int){}voidUnscribe(constboost::function&f){std::map>map;map[0]=ff;if(map[0]==f){}}Unscribe(ff);我希望能够比较两个具有相同签名的boost::function。我应该修改什么以使此代码可编译? 最佳答案 你不能。阅读boostfunctionFAQ的第一个条目:Whycan'tIcompareboost::functionobjectswithoperator==oroperator!=?Comparisonbetweenbo