草庐IT

lambda表达式

全部标签

c++ - C++11 中 lambda 的内存管理

有人可以描述为什么这段代码不起作用(在从调用返回之前在GCC4.7.3上出现段错误)吗?#include#include#includeusingnamespacestd;templateautomemo(constF&x)->std::function{typedefdecltype(x())return_type;typedefstd::functionthunk_type;std::shared_ptrthunk_ptr=std::make_shared();*thunk_ptr=[thunk_ptr,&x](){cerr我最初的假设:每个std::function是对表示闭包的

c++ - 为什么我不能直接将 to_lower_copy 传递给 transform 而不是将其包装在 lambda 中?

我正在尝试使用boost::to_lower_copy和std::transform来小写一堆字符串。如下,变体1,使用lamdba工作;变体2还可以证明这是编译器选择的正确模板重载。但是lambda很傻——它所做的只是将单个参数转发给boost::to_lower_copy。但是变体3,直接使用函数模板不会编译,即使我实例化它。我错过了什么?我有clang版本3.3(tags/RELEASE_33/rc3),使用libstdc++-4.8.1-1.fc19.i686和boost-1.53​​.0-14.fc19.i686。vectorstrings={"Foo","Bar"};vec

c++ - boost 正则表达式 : [:alpha:] and accented characters

我正在尝试使用Boost将字符串中的每个非字母字符替换为"":std::stringsanitize(std::string&str){boost::regexre;re.imbue(std::locale("fr_FR.UTF-8"));re.assign("[^[:alpha:]]");str=boost::regex_replace(str,re,"");returnstr;}intmain(){std::stringtest="(ça)/.2424,@vatrèsbien?";cout结果是avatrsbien但我想得到çavatrèsbien。我错过了什么?

c++ - 线程池的 lambda 函数内部的编译器错误变量 "Not captured"

我正在学习C++中的多线程并尝试设置线程池,但我收到一个编译器错误,提示“错误:‘mapperNodes’未被捕获”和“错误:‘command’未被捕获”。我读过一些关于使用“this”来捕获lambda中的变量的内容,但到目前为止没有任何效果。如何在下面的代码中使用线程池lambda函数中的command和mapperNodes变量?voidMapReduceServer::spawnMappers()throw(){vectormapperNodes(nodes);random_shuffle(mapperNodes.begin(),mapperNodes.end());strin

c++ - 通用 lambda 不能在命名空间中使用?

考虑以下代码#include#includenamespaceA{templatestructX{usingFunction=std::function;staticFunctionf;};templatetypenameX::FunctionX::f=[](auto){return42;};}intmain(){std::cout::f(0);}GCC和clang都接受此代码,但MSVC(已测试版本19.00.23506)给出:errorC2888:'auto::operator()(_T1)const':symbolcannotbedefinedwithinnamespace'A'

c++ - 常量字符数组的获取字符串长度函数(strlen)不是常量表达式

直接看简化代码(编译:GCC6.3.0)#include#includeusingnamespacestd;intmain(intarga,char*argv[]){constcharcs[]="Hello";//defineaconstantc-stylestringconstexprsize_tnewSize=strlen(cs)+strlen("");//Errorreturn0;}编译器产生错误:strlen(((constchar*)(&cs)))不是常量表达式但是,当我将c字符串定义移动到全局范围时,问题就解决了。....constcharcs[]="Hello";intm

c++ - 如何使用 lambda 作为 std::unique_ptr 的删除器?

检查以下设计的程序:#include#includetemplateusingUniPtr=std::unique_ptr>;int*alloc(){returnnewint;}UniPtrfunc(){autodealloc=[](int*p){deletep;};returnUniPtr{alloc(),dealloc};}intmain(){autop=func();return0;}来自std::functionconstructormanual,我认为构建std::function对象可能会抛出异常,即使这个比例很低:UniPtrfunc(){autodealloc=[](i

c++ - lambda 如何在 MSVC2017 15.9.3 with/std :c++17? 中使用静态本地错误返回值

下面的示例代码打印来自lambda函数的值,该函数简单地递增并返回静态局部计数器变量的值。它打印0,1和2,3正如预期的那样,gcc和C++17的clang。但在VisualStudioCommunity201715.9.3中没有/std:c++17设置-它打印0,0和2,3相反。#includeintmain(){autof=[]{staticinti=0;returni++;};constintv1=f();//Expectv1=0constintv2=f();//Expectv2=1//Printsthewrongvalues(MSVC15.9.3with/std:c++17)s

c++ - 开关错误::不能出现在常量表达式中

这是一个奇怪的...我正在玩一些减压算法。我没有通过charbuffer[]并循环直到找到buffer[i]中的停止位,而是尝试使用一些位掩码技术,但使用chars.我有以下示例://Ina*.hfileconstcharch='\x81';//ToavoidEndianessunionCharUInt{charsz[4];unsignedintu;};//Legalbecausechar[]isdeclaredbeforeuint32intheunionconstCharUIntMask1={'\x81','\x0','\x0','\x81'};constCharUIntMask2=

c++ - 如何使用 Eigen 3 表达 "<array-of-true-or-false> = <array> <= <scalar>"?

我正在使用Eigen3模板库将一些MATLAB代码移植到C++,我正在为这个常见问题寻找一个好的映射MATLAB习惯用法:K>>[12345]因此,比较数组和标量,返回具有相同形状的bool数组。我知道Eigen的Array类有系数比较运算符,但如果我正确地解释了文档,它们只适用于另一个数组;不是标量值。是否有一些我错过的选项可以执行与标量的比较?还是失败了,一个很好的惯用方法来创建一个形状适当的Array填充表达式RHS的标量值? 最佳答案 感谢#eigenIRCchannel的ChriSopht_:VectorXdcompare