草庐IT

c++ - 基于自动范围的结构化绑定(bind)与 vector

我正在尝试循环一个元组vector:std::vector>tupleList;通过使用基于范围的for循环和结构化绑定(bind):for(auto&&[x,y,z]:tupleList){}但是VisualStudio201715.3.5报错:cannotdeduce'auto'type(initializerrequired)但以下确实有效:for(auto&&i:tupleList){auto[x,y,z]=i;}这是为什么? 最佳答案 它确实有效,但智能感知不使用相同的编译器:因此,即使编辑器中显示红线和错误,它也会使用I

c++ - 绑定(bind)到函数参数的引用会延长该临时对象的生命周期吗?

我有这段代码(简化版):constint&function(constint¶m){returnparam;}constint&reference=function(10);//usereference我不太确定C++03标准$12.2/5措辞的程度Thetemporarytowhichthereferenceisboundorthetemporarythatisthecompleteobjecttoasubobjectofwhichthetemporaryisboundpersistsforthelifetimeofthereference...在这里适用。上面代码中的ref

c++ - std::function 和 std::bind 做动态内存分配吗?

如果我这样做:-classThing{...voidfunction(conststd::string&message);};std::list>work;在“Thing”的一些成员中work.push_back(std::bind(&Thing::function,this,"Hello"));调用std::bind或使用std::function是否会导致使用new或其他方式进行任何动态内存分配?或者所有的存储空间都是在编译时分配的?如果标准没有说明任何内容,那么在visualstudio2012中呢,因为我的程序只需要在那里构建,为了提高效率,我可能需要在我考虑使用这种机制的地方

c++ - 为什么可以间接地将右值绑定(bind)到左值引用而不是直接绑定(bind)?

根据我的阅读和了解,您不能将右值表达式绑定(bind)到左值引用。然而,我所看到的是,您可以将右值绑定(bind)到右值引用,并且由于命名的右值引用本质上是左值,因此您可以将它绑定(bind)到左值引用。不允许将右值绑定(bind)到左值引用背后的原因是什么。是否出于优化目的?举个例子:#includeusingstd::cout;voidbar(int&b){cout 最佳答案 这是C++的基本规则,它可以防止错误:intfoo();int&x=3;//whoopsint&y=foo();//whoops(sometimes)“右

c++ - 一个 VS2010 错误?允许在没有警告的情况下绑定(bind)对右值的非常量引用?

stringfoo(){return"hello";}intmain(){//belowshouldbeillegalforbindinganon-const(lvalue)referencetoarvaluestring&tem=foo();//belowshouldbethecorrectoneasonlyconstreferencecanbebindtorvalue(mostimportantconst)conststring&constTem=foo();}GCC是给出编译错误的好工具:std::string&类型的非常量引用从std类型的临时值初始化无效::字符串VS2008

C++/Xcode 套接字绑定(bind)()错误

当尝试处理来自sys/sockets.hbind()的返回值(应该是int)时,Xcode编译失败并出现错误二进制表达式的无效操作数(“__bind”和“int”)它似乎为bind()提取了不正确的函数定义,因为当我从上下文菜单中选择“跳转到定义”时,它给了我三个选项:2个来自functional.cpp的定义,1个来自sys/sockets。H。我的代码中包含sys/sockets,如何强制编译器使用正确的定义? 最佳答案 使用::bind或去掉usingnamespacestd;。

c++ - 将左值绑定(bind)到右值引用——g++ 错误?

作为对anotherquestion的回答我想贴出下面的代码(也就是我想贴出基于这个思路的代码):#include#include//std::is_same,std::enable_ifusingnamespacestd;templatestructBoxed{Typevalue;templateBoxed(Argconst&v,typenameenable_if::value,Arg>::type*=0):value(v){wcoutv){}intmain(){inti=5;function(i);//但是,虽然MSVC11.0在最后一次调用时阻塞,但IHMO应该如此,而MinGW

c++ - 为什么第一个函数调用绑定(bind)到第一个函数?

这个问题在这里已经有了答案:Whydoespointerdecaytakepriorityoveradeducedtemplate?(1个回答)关闭6年前。为什么第一个函数调用(cm(car);)绑定(bind)到第一个函数?我知道第二次调用绑定(bind)到第二个函数,因为它不是模板,尽管两者都是完美匹配。如果第一个函数定义为固定数组长度的非模板,如:voidcm(constchar(&h)[8]){cout然后它再次被选中而不是第二个(第二个调用将以这种方式模棱两可)。代码:templatevoidcm(constchar(&h)[N]){std::cout输出:const(&)[

c++ - const 引用是否绑定(bind)到另一个从临时悬挂引用转换而来的引用?

下面是代码片段:#includeusingnamespacestd;structB{intb;~B(){cout在thisonlinecompiler输出是destructBdestructB1因此返回值似乎比cout操作更早析构。所以instance似乎是一个悬空引用。如果我们把constB&instance=(constB&)func();改成constB&instance=func();,那么结果就是destructB1destructB作为补充,如果我在vs2015中测试代码,那么输出的是最后一个。但是,如果在gcc(before4.6)中测试,输出是前者,但在4.6之后的版本

c++ - 如何将 boost::bind 与不可复制的参数一起使用,例如 boost::promise?

有些C++对象没有复制构造函数,但有移动构造函数。例如,boost::promise。我如何使用移动构造函数绑定(bind)这些对象?#includevoidfullfil_1(boost::promise&prom,intx){prom.set_value(x);}boost::functionget_functor(){//boost::promiseisnotcopyable,butmovableboost::promisepi;//compilationerrorboost::functionf_set_one=boost::bind(&fullfil_1,pi,1);//co