草庐IT

lambda表达式

全部标签

c++ - 成员初始化中表达式的求值是否相对于彼此排序?

具体来说,假设我有:structX{X(inti){cout我知道成员的构造函数保证按照它们在struct中定义的顺序被调用,所以0将在1之前打印。但是如何评价他们的论点呢?是否保证:f0g1?或者,也许,fg01和gf01也是有效输出吗?对标准的引用表示赞赏。 最佳答案 在C++11draftstandard中,每个成员初始值设定项都是一个完整表达式,因此所有副作用都必须在下一个评估之前生效。12.6.2初始化基和成员段7说:[...]Theinitializationperformedbyeachmem-initializerc

c++ - 哪些提升类型用于 switch-case 表达式比较?

以下程序在使用不同的编译器编译时打印“unknown”。为什么会这样?#include"stdio.h"constcharOPTION=(char)(unsignedchar)253;intmain(intargc,char*argv[]){unsignedcharc=253;switch(c){caseOPTION:printf("option\n");break;default:printf("unknown\n");break;}return0;}在查看C++标准(N36902013-05-05)时,我看到了switch的子句:6.4.2Theswitchstatement2Th

c++ - static_casting a constexpr void* 的结果是常量表达式吗?

clang正在拒绝gcc允许的这段代码:intmain(){staticconstexprconstvoid*vp=nullptr;staticconstexprconstchar*cp=static_cast(vp);}具有以下内容:error:constexprvariable'cp'mustbeinitializedbyaconstantexpressionstaticconstexprconstchar*cp=static_cast(vp);阅读完N3797中的最终list后5.9/2我没有看到任何禁止在常量表达式中使用static_cast的内容。我是在看错地方还是误读了什么

c++ - 了解常量表达式

我试图理解常量表达式概念(来自c++reference):structS{staticconstintc;};constintd=10*S::c;//notaconstantexpression:S::chasnopreceding//initializer,thisinitializationhappensafterconstconstintS::c=5;//constantinitialization,guaranteedtohappenfirst在我们定义它之前,为什么S::c不是常量表达式。虽然它被声明为静态const数据成员...... 最佳答案

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++ - 为什么 `auto&` 不能绑定(bind)到 volatile 右值表达式?

考虑下面的代码:intmain(){inti{};auto&c=static_cast(i);//(1)auto&v=static_cast(i);//(2)}(1)编译成功,(2)不被接受:error:volatilelvaluereferencetotype'volatileint'cannotbindtoatemporaryoftype'volatileint'为什么auto不能变成volatileint?为什么auto&可以变成constint并绑定(bind)到constint&&?是因为auto&实际上绑定(bind)到一个在赋值右侧创建的临时对象吗?但是,为什么auto&

c++ - 为什么在增加 -fconstexpr-steps 后无法解析常量表达式?

以下面的constexpr为例:#includeconstexprintfib(constinti){if(i==0)return0;if(i==1)return1;returnfib(i-1)+fib(i-2);}intmain(){std::cout尽管是constexpr,但它不会在编译时求值。我学到的执行编译时评估的技巧如下:#include#include#defineCOMPILATION_EVAL(e)(std::integral_constant::value)constexprintfib(constinti){if(i==0)return0;if(i==1)retu

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++ - 如何模拟可变参数函数中的折叠表达式?

Microsoft似乎对FoldExpressions的支持很慢,我正在努力寻找一种方法来模拟它。我有以下缩减示例。由于不支持折叠表达式,因此无法在VisualStudio2017上编译。知道如何模拟吗?templatevoidget(weak_ptrawdb_,Args&&...args){(*awdb_.lock() 最佳答案 在折叠表达式之前,有一个expandertrick:templatevoidget(weak_ptrawdb_,Args&&...args){if(autodb=awdb_.lock()){usingex

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([