草庐IT

lambda表达式

全部标签

c++ - C++函数调用是表达式吗?

一个函数产生一个结果,可以用作另一个函数参数的参数。所以,函数调用是这样的:f(1,2,3)被认为是“表达”?谢谢。 最佳答案 C++标准(N3376,§5.1)将表达式指定为:Anexpressionisasequenceofoperatorsandoperandsthatspecifiesacomputation.Anexpressioncanresultinavalueandcancausesideeffects.在同一部分(§5.2.2)中:Afunctioncallisapostfixexpressionfollowedb

c++ - 传递 Lambda 时,Visual Studio 2017 中的什么扩展消除了 "bool"与 "std::function"的歧义?

以下代码在VisualStudio2017中使用MSVC编译器编译,但在GCC或Clang中编译失败。#include#includevoidfunctionTest(std::function){std::cout要解决这个问题,我们可以像这样使用enable_if:#include#includevoidfunctionTest(std::function){std::cout::value>::type>voidfunctionTest(BOOL_TYPE){std::cout或者我可以通过引入用户类型而不是bool来消除歧义(如果构造函数存在歧义问题,您需要这样做):#incl

c++ - (C++14) lambda 数组 : error: 'name' declared as array of 'auto'

我很难解决这个错误。我承认,我是C++的新手,我的困难来自于不理解错误消息。代码如下:autoselectionFuncs[8]={[&](constVector3&min,constVector3&max){returnmax.x_==seamValues.x_||max.y_==seamValues.y_||max.z_==seamValues.z_;},[&](constVector3&min,constVector3&max){returnmin.x_==seamValues.x_;},[&](constVector3&min,constVector3&max){returnm

c++ - 数组声明中的常量表达式

C++Primer说Arraydimensionmustbeknownatcompiletime,whichmeansthatthedimensionmustbeaconstantexpression单独指出unsignedcount=42;//notaconstantexpressionconstexprunsignedsize=42;//aconstantexpression然后我希望下面的声明失败a[count];//IsanerroraccordingtoPrimer然而,事实并非如此。编译并运行良好。同样奇怪的是++count;在数组声明之后也不会导致任何问题。在g++4.7

C++11 可变参数模板和逗号分隔表达式等价

在可变参数模板中...运算符将参数包扩展为一系列以逗号分隔的参数(以最简单的形式)。我的问题是:为什么以逗号分隔的多个参数调用some_function()并使用...运算符调用它却不行?我说的是这段代码:templateinlinevoidexpand(Args&&...args){some_function(22),some_function(32);//Workssome_function(args)...;//Doesn'twork-ERROR}这两行不应该产生相似的输出吗? 最佳答案 正如在另一个答案中所说,通过扩展参数包

c++ - 当 lambda 作为参数传入时推导模板参数

我正在尝试编写函数func这样编译器就可以推断出模板参数,当我传入std::function时它会起作用,但不适用于lambda:templateTResultfunc(std::functionf){returnTResult();}intmain(){//VisualStudio2013intresult=func([](){//error:'TResultfunc(std::function)':couldnotdeducetemplateargumentfor'std::function'from'main::'return100;});std::functiontestFun

c++ - 编译器如何根据优先级和关联性来解释这个表达式?

这是C++Primer5th的练习:Exercise4.33:Explainwhatthefollowingexpressiondoes(Page158):someValue?++x,++y:--x,--y代码:boolsomeVlaue=1;intx=0;inty=0;someVlaue?++x,++y:--x,--y;std::cout我尝试了Gcc4.81和Clang3.5,都给了我:10Presstoclosethiswindow...为什么不是1和1?任何人都可以解释它是如何解释的吗? 最佳答案 由于逗号运算符的优先级非常

c++ - 为什么 std::result_of 不适用于 lambda?

我设法将我的案例简化为以下最简单的代码:#includeautocall(constauto&f)->typenamestd::result_of::type{returnf();}intmain(){returncall([]{return0;});}gcc-4.9.2和gcc-5.0.0都不编译!两者都认为“调用”应该返回一个lambda函数!不要弄清楚“调用”返回一个int。这是编译器中的错误还是我的C++关闭了?非常感谢。 最佳答案 您的代码不是有效的C++,因为函数参数类型不能是auto,此语法已为ConceptsLite

c++ - lambda 参数可以隐藏非捕获变量吗?

我有一个看起来像这样的代码-它被大大简化了,但是这个片段编译并表现出相同的行为:templatefloatFloatSelect(constfloatin_value,TFunc&&Predicate){returnstd::forward(Predicate)(in_value)?in_value:0.0f;};voidDisplayFloatSelect(){constfloatvalue=FloatSelect(-1.0f,[](constfloatvalue){returnvalue>0.0f;});std::cout启用-Wshadow后,编译器会发出以下警告(如here所示

c++ - 字符是否在 C 表达式中自动提升?

我对我的一位同事发表了声明,内容是:"charsareautomaticallypromotedtointegersinCexpressions,andthat'sfineforperformancesinceCPUsworkfastestwiththeirnaturalwordsize.我相信由于字符的等级,字符提升行为已在标准中的某处说明。这是我得到的回复:"Charactersarenotdefaultpromotedtoaninteger.Theregistersizeis32bit,butmultiplebytevaluesinarowcanbepackedintoasing