草庐IT

generic-lambda

全部标签

c++ - lambda 表达式中引用捕获和非显式捕获的 constexpr 变量之间的区别

这个问题Accesstoconstexprvariableinsidelambdaexpressionwithoutcapturing回答了为什么下面示例中的ref-capture不是严格必要的。但另一方面,如果它被捕获,则会出现错误。错误似乎是由foo()的递归性质触发的。templateconstexprintbar(constT&x){//NOK//constexprintbar(Tx){//OKreturnx;}templateintfoo(constT&l){constexprautox=l()-1;autoy=[&]{returnbar(x);};//ifref-captu

c++ - 使用带有 lambdas/bind 的成员函数实现中间件函数

我有一个功能,Post(),它有两个参数-astd::string监听请求的路径,以及std::function处理传入的请求。请注意,我无法修改Post().例如:m_server.Post("/wallet/open",[this](autoreq,autores){std::cout我正在尝试通过中间件函数传递请求,然后转发到处理函数。处理函数和中间件函数是成员函数。Post()绑定(bind)的设置发生在同一类的成员函数中。这个有效:m_server.Post("/wallet/open",[this](autoreq,autores){autof=std::bind(&Api

c++ - 在 Qt 信号和槽中使用 lambda 语法并访问传递的参数

我有一个具有此签名的信号的类://CLASSAsignals:voidrequestToChangeRange(voltage_range_evr,current_range_ecr,uint16_tbits);还有另一个类有这样一个插槽(注意额外的参数)//CLASSCpublicslots:voidhandleRequestRangeChange(voltage_range_evr,current_range_ecr,uint16_tbits,uint16_tlimiter);然后我有一个类“B”,它充当所有其他类的交汇点。当“A”类发出信号时,“C”类应将其重定向到“B”类。但是

C++ lambda 表达式无法编译

我正在尝试使用lambda表达式cin循环索引值:#includeusingnamespacestd;intmain(){for(inta,([](int&b){cin>>b;})(a);a这些是我在ubuntu上使用g++4.5编译时的错误:forLoopAndCinTest.c++:Infunction‘intmain()’:forLoopAndCinTest.c++:5:14:error:expectedunqualified-idbefore‘[’tokenforLoopAndCinTest.c++:5:14:error:expected‘)’before‘[’tokenfor

C++11 for_each 和 lambdas 优化

我正在测试以下代码:#include#include#include#includeintmain(intargc,char*argv[]){std::vectorv(10000000);clock_tthen=clock();if(argc我正在使用g++4.6编译它,没有任何优化标志,这是我得到的:[javadyan@myhostexperiments]$./a.out260000[javadyan@myhostexperiments]$./a.outaaa330000[javadyan@myhostexperiments]$使用-O1优化会产生以下(意料之中的)结果:[javad

c++ - 如何检测通用 lambda 在 C++ 14 中是否不可编译?

我在检测通用lambda的实例何时格式正确但不可编译时遇到问题,检测它让我很困惑:#includeclassfuture{public:intget()&{return5;}};//GetsthereturntypeofF(A),returninganot_well_formedtypeifnotwellformedtemplatestructget_return_type{structnot_well_formed{};templatestaticnot_well_formedtest(...);templatestaticautotest(_F&&f)noexcept(noexce

c++ - std::transform with lambda: 跳过一些项目

我有一些C++11代码,比如std::vectornames;std::mapfirst_to_last_name_map;std::transform(names.begin(),names.end(),std::inserter(first_to_last_name_map,first_to_last_name_map.begin()),[](conststd::string&i){if(i=="bad")returnstd::pair("bad","bad");//Don'tWantThiselsereturnstd::pair(i.substr(0,5),i.substr(5,

c++ - 将 lambda 作为参数传递时重载函数

我正在尝试在返回参数为void或T时实现模板函数。我使用sfinae尝试了上述代码的不同变体,但仍然不确定在lamdba是函数参数的情况下这通常是否可行。以下代码无法编译:#includetemplateTApply(conststd::function&func){returnfunc();}templatevoidApply(conststd::function&func){func();}intmain(intargc,char*argv[]){inti1=Apply([](){return10;});boolb1=Apply([](){returntrue;});Apply([

c++ - 在 namespace 内的 lambda 中使用时找不到运算符重载

以下不编译(使用Clang5.0.0/gcc7.3,std:C++11):Clang中的错误信息:错误:二进制表达式的无效操作数(std::vector>和std::vector>)#include#includenamespacens{usingMyType=std::vector;}//namespacensusingns::MyType;MyType&operator+=(MyType&lhs,constMyType&rhs){for(inti=0;i;Funcoperator+(constFunc&lhs,constFunc&rhs){return[lhs,rhs](){aut

c++ - Lambda 类型推导

autodothings=[](longposition){autovariable;/*dothings*/returnvariable;};floatx=dothings(1l);chary=dothings(2l);基本上,我很好奇的是,lambda内部的变量是否有可能以任何方式推断返回值分配给的类型,在这种情况下它是float和char。有没有等同于模板类型名的东西?谢谢。 最佳答案 这可以做到,但它a)有点复杂,b),在99.9%的情况下,这并不是一个好主意。以下是您的操作方式。您可以根据将表达式分配给的类型执行某些操作的