草庐IT

function-call-operator

全部标签

c++ - std::function 的模板参数(签名)不是其类型的一部分吗?

给定以下代码,歧义背后的原因是什么?我可以规避它还是必须保留(烦人的)显式转换?#includeusingnamespacestd;inta(constfunction&f){returnf();}inta(constfunction&f){returnf(0);}intx(){return22;}inty(int){return44;}intmain(){a(x);//Callisambiguous.a(y);//Callisambiguous.a((function)x);//Works.a((function)y);//Works.return0;}有趣的是,如果我注释掉a()功

c++ - <function> 不是 <class> 的成员

我已将我的函数“Credit”声明为带有一些参数的私有(private)成员。我的观察是,每当我尝试不带任何参数进行编译时,编译器都会成功编译。但是一旦我用函数中的参数进行编译,编译器就会报错'Transaction::Credit'isnotamemberof'Transaction'这是我的代码classTransaction:publicMenu{private:voidCredit(intdepost);//{return0;}public:voidDeposit();voidWithdraw(){}voidTransfer(){}};voidTransaction::Depo

c++ - 为什么不使用强制转换语法调用 "operator void"?

在玩thisanswer时通过userGMan我制作了以下代码片段(使用VisualC++9编译):classClass{public:operatorvoid(){}};Classobject;static_cast(object);(void)object;object.operatorvoid();通过调试器后,我发现转换为void不会调用Class::operatorvoid(),只有第三次调用(显式调用运算符)实际上调用了运算符,这两个转换什么都不做。为什么operatorvoid没有用强制转换语法调用? 最佳答案 在§1

c++ - 为什么 g++ 声明某些 valarray<double> o 有 "no matching function for call cbegin(o)"?

请考虑以下代码:usingcustom_t=std::valarray;custom_to;unsignedacc=std::accumulate(std::cbegin(o),std::cend(o),0);g++-5说Nomatchingfunctionforcalltocbegin(custom_t&)如果我改用std::begin(o)和std::end(o),一切正常。这是编译器错误吗?代码使用VisualStudio2015编译。 最佳答案 这是一个libstdc++错误,我刚刚创建了https://gcc.gnu.or

c++ - "warning: operation of ... may be undefined"用于三元运算——不是 if/else block

这个问题在这里已经有了答案:Undefinedbehaviorandsequencepoints(5个答案)关闭6年前。这是我的代码:intmain(){staticinttest=0;constintanotherInt=1;test=anotherInt>test?test++:0;if(anotherInt>test)test++;elsetest=0;return0;}这是我构建它时产生的警告:../main.cpp:15:40:warning:operationon‘test’maybeundefined[-Wsequence-point]test=anotherInt>te

c++ - std::function 参数列表和 typedef

我有一个类似这样的typedef:typedefstd::functionMyFunction;在我的代码中某处是这样使用的:MyFunctionfunc=[](intarg1,floatarg2){/*dosometing*/};问题是,每次我更改函数的参数数量(例如,我添加第三个参数chararg3)时,我都被迫在我使用MyFunction的所有代码中更新它(即使这些根本不使用参数。而且我懒得那样做。有没有办法从它的类型中获取std::function的参数列表?(我的意思是)这样函数创建看起来像那样?:MyFunctionfunc=[](MyFunction::args){/*d

c++ - 调用时为 std::function 分配新值

inti=9;struct_variable.f=[i](Tstruct_variable&){do_something_with_capture_variable(i);...struct_variable.f=another_compatible_std_function;//dosomethingelse,butneverusecapturedvariableafterhere...};struct_variable.f(struct_variable);lambda函数被保存为成员struct_variable.f(也是类型std::function),在回调中,struct_

c++ - 为什么 std::shared_ptr 提供 operator<<?

std::shared_ptr提供operator它只是写出它的地址。没有operator>>只记录地址,不记录内容。我想知道它在哪些情况下有用。 最佳答案 因为是一个潜在有用的东西在原始指针上执行。这是安全的,原始指针就是这样做的,shared_ptr在某些情况下应该用于替换原始指针。相比之下,>>很少有意义。与原始指针不同,将指针值存储在共享指针中会取得它的所有权。我可以some_stream>>raw_ptr除非我用ptr做些什么没有任何问题;有点奇怪,但没有立即中断。对shared_ptr做同样的事情只有在极其深奥的情况下才

C++ 在结构中重载 operator()

这是来自Box2d物理引擎的b2Math.h的代码。structb2Vec2{...///Readfromandindexedelement.floatoperator()(inti)const{return(&x)[i];}///Writetoanindexedelement.floatoperator()(inti){return(&x)[i];}...floatx,y;}为什么我们不能只使用SomeVector.x和SomeVector.y来读/写vector坐标?return(&x)[i];行实际上是如何工作的?我的意思是,对结构的x组件的引用之后的数组括号[]对我来说并不清楚

c++ - 为什么可以将 std::bind 分配给参数不匹配的 std::function?

我有如下代码:#include#includeusingnamespacestd;voidF(intx){coutf1=std::bind(F,std::placeholders::_1);f1(100);//Thisworks,willprint100.intx=0;std::functionf2=std::bind(F,x);f2();//Thisworks,willprint0.std::functionf3=std::bind(F,x);f3(200);//BUTWHYTHISWORKS??????Itprints0.return0;}我的编译器信息是:AppleLLVM版本6