草庐IT

boost-lambda

全部标签

c++ - 对于 C++ lambda,按引用捕获引用的规则是什么?

对于C++lambda,当您通过引用捕获引用时会发生什么?您是在捕获对堆栈上本地对象的引用(引用本身),还是对被引用对象的引用?例如在下面的代码中:int&TestClass::returnReference(){staticinti=0;returni;}std::functionTestClass::testFunction(){int&memberRef=this->someIntMember;int&intRef=returnReference();autolambda=[&]{//Whathappenswhenyoucaptureareferencebyreference//

c++ - lambda 的仅类型模板参数

假设我有这个结构:structFoo{operatorint(){return11;}operatorunsignedint(){return22;}}foo;当此结构被转换为int时,它返回11,但当转换为unsignedint时,它返回22。使用普通函数,我可以使用模板和getter函数来选择:templateTget(){return(T)foo;}现在,当像get()这样调用这个函数时它会返回11,但是当像get()这样调用它时它会返回22.直到现在,当我尝试使用lambda时,一切都很好:autolambda=[](autotype){return(decltype(type

c++ - boost 侵入式指针

我对boost的侵入式指针有点困惑。定义说:"Everynewintrusive_ptrinstanceincrementsthereferencecountbyusinganunqualifiedcalltothefunctionintrusive_ptr_add_ref,passingitthepointerasanargument.Similarly,whenanintrusive_ptrisdestroyed,itcallsintrusive_ptr_release;thisfunctionisresponsiblefordestroyingtheobjectwhenitsre

c++ - Lambda 按值捕获和 "mutable"关键字

lambda中对关键字mutable的需求是造成极大困惑的根源。考虑代码:intx=10;functionlambda=[=]()mutable{x++;cout输出:Beforelambda:x=10Insidelambda:x=11Afterlambda:x=10正如我们所见,变量x在lambda之后保持不变,因此没有副作用。但是,如果我们“忘记”了关键字mutable,就会出现错误。作为C++中默认的按值传递参数,对我来说需要mutable关键字没有意义。有人可以编写(即使是伪代码)编译器生成的类来代替lambda吗?谢谢 最佳答案

c++ 可以通过引用传递临时 lambda(适用于 msvc/windows 但不适用于 gcc/linux)吗?

假设我有以下代码片段://globalvariablestd::threadworker_thread;//Templatefunctiontemplatevoidstart_work(Functor&worker_fn)//lambdapassedbyref{worker_thread=std::thread([&](){worker_fn();});}这样调用:voiddo_work(intvalue){printf("Hellofromworker\r\n");}intmain(){//Thislambdaisatemporaryvariable...start_work([do

c++ - Lambda 通过引用捕获并在槽中使用

我有与此类似的C++14代码:voidC::f(){int&ref=this->x;autolb=[&ref](){/*dostuffwith"ref"*/};if(foobar){//calllbwhensignalfires.connect(object,&D::signal,[&lb](){lb();});}else{lb();}}我知道当我使用lb时,this仍然有效。但是ref和lb呢?上面的代码是否有任何悬空引用?我发现了类似的问题(here,there,...)但我无法得出结论。 最佳答案 lb有自动存储,所以当这个函

c++ - 试图摆脱 C++ boost 警告

每当我在我的项目中加入boost时,我都会收到一百万条这样的警告。有谁知道我怎样才能摆脱警告?../depends\boost/config/abi_prefix.hpp(19):warningC4103:'depends\boost\config\abi_prefix.hpp':alignmentchangedafterincludingheader,maybeduetomissing#pragmapack(pop)我知道我可以执行#pragma来禁用警告,但我想知道这些警告的原因。 最佳答案 原因是boost不会在每个需要打包数

c++ - 在 shared_from_this() 中 boost weak_ptr_cast

我正在使用boost的共享指针,并使用enable_shared_from_this来启用返回指向this的共享指针。代码如下所示:classfoo:publicboost::enable_shared_from_this{boost::shared_ptrget(){returnshared_from_this();}}为什么shared_from_this会抛出weak_ptr_cast异常? 最佳答案 如果您在堆栈上声明了foo,那么就没有其他指向foo的共享指针。例如:voidbar(){foofooby;fooby.get

C++,boost asio,接收空终止字符串

如何使用boost::asio库从套接字中检索以null结尾的字符串? 最佳答案 m_socket=boost::asio::ip::tcp::socket(io_service);boost::asio::streambufreplyBuf;......boost::asio::read_until(m_socket,replyBuf,'\0');如果你想将streambuf转换为字符串:std::stringretVal((std::istreambuf_iterator(&replyBuf)),std::istreambuf_

c++ - 如何为 boost::bind 强制模板函数重载?

我正在尝试通过使用boost::bind和boost::contains为std::find_if创建谓词(来自提升/算法/字符串库)。以下代码段显示了我如何尝试实现此目的的两种方式。#include#include#include#include#includeintmain(intargc,char**argv){std::strings1("hellomom");std::strings2("byemom");boost::functionf=&boost::contains;std::coutcontain_hello=boost::bind(boost::contains,_