草庐IT

send_this_email

全部标签

c++ - 对成员函数的模糊调用以在 lambda 中捕获 this

我在尝试为捕获的this调用lambda内部的成员函数时遇到了问题。该函数有const和非const版本,它以类型为​​模板。下面的代码演示了错误:structTEST{templatevoidtest(){}templatevoidtest()const{}TEST(){[this](){test();}();}};消息:http://rextester.com/MLU2098source_file.cpp(13):errorC2668:'TEST::test':ambiguouscalltooverloadedfunctionsource_file.cpp(7):note:coul

c++ - 你应该如何用 shared_ptr 返回 *this?

另请参阅:Similarquestion下面的代码显然是危险的。问题是:您如何跟踪对*this的引用?usingnamespaceboost;//MyClassDefinitionclassMyClass{public:shared_ptrcreateOtherClass(){returnshared_ptrOtherClass(this);//baaad}MyClass();~MyClass();};//OtherClassDefinitionclassOtherClass{public:OtherClass(const*MyClassmyClass);~OtherClass();}

c++ - 为什么编译器说 : 'enable_if' cannot be used to disable this declaration

templateusingEnable_if=typenamestd::enable_if::type;classDegree;templateconstexprinlineboolIs_Degree(){returnstd::is_base_of::value;}classDegree{public:std::size_tinDeg=0;};templateclassVertex:publicSatellite{public:explicitVertex(intnum):n(num){}private:std::size_tn;};templateclassEdge{public:/

c++ - Boost::asio async_write_some 与 async_send

我刚才才注意到boost::asio中的async_write_some和async_send(第二次重载)函数是完全一样的:async_write_some定义:...templateBOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,void(boost::system::error_code,std::size_t))async_write_some(constConstBufferSequence&buffers,BOOST_ASIO_MOVE_ARG(WriteHandler)handler){//Ifyougetanerroronthefo

c++ - 有没有办法强制使用 "this->"用于 clang-format/clang-tidy 中的类成员/方法?

我到处搜索,但我可能用错了术语。我还没有为此找到选项。我唯一发现的是这个未回答的问题(但是有点宽泛):CPPlint:Canyouenforceuseofthisforclassvariables?. 最佳答案 鉴于existingoptions,我不相信这在clang-format中是可能的,future也不会。主要原因是程序的工作方式。它不会将C++代码解析为AST,而是将文本标记化而不需要包含(定义它的成员和全局变量)而不是编译数据库(影响定义、包含路径……)它是甚至可以给它一段代码并重新格式化。从问题的性质来看,如果它可以存

C++篇--类大小计算、this指针

文章目录一、类大小计算二、this指针一、类大小计算类的大小是只计算它的成员变量或者自定义成员,不会计算它的成员函数大小。#includeusingnamespacestd;classA1{public: voidfun() { _a=1; _b=2; } int_a; int_b;};intmain(){ coutsizeof(A1)endl;//8字节,为何是8字节 return0;}光是类中成员变量_a,_b所占的字节大小就为8了,但是类中还要成员函数,为何还是8字节难道成员函数没有在类之中,对的类成员函数在公共代码区的,不同对象的成员数据是私有的每个人有自己的成员数据但是成员函数

c++ - sizeof(*this) 和结构继承

假设我有一个如下所示的结构:structParentStruct{virtualvoidXYZ(){getSize(sizeof(*this));}intmemberX;}还有另一个继承父结构的struct:structChildStruct:publicParentStruct{intmemberY;intmemberZ;}假设sizeof(int)==4,是否可以将12的值传递给函数getSize()从子结构调用(我目前得到的值是4)?我宁愿不必覆盖所有子结构中的XYZ(),因为我会有很多子结构。 最佳答案 正如其他人所说,th

c++ - 错误 : passing const xxx as this argument of xxx discards qualifiers

我在将仿函数从Windows移植到Linux时遇到问题。(传递给STL::map以进行严格弱排序的仿函数)原文如下:structstringCompare{//Utilizedasafunctorforstl::mapparameterforstringsbooloperator()(stringlhs,stringrhs){//Returnstrueiflhs由于linux不支持_stricmp而是使用strcasecmp,我将其更改为:structstringCompare{booloperator()(stringlhs,stringrhs){//Returnstrueiflhs

c++ - 将 "this"称为 shared_ptr?

这个问题在这里已经有了答案:std::shared_ptrofthis(2个答案)关闭8年前。我正在学习C++11特性,特别是shared_ptr,我在引用this并将其用作其他类的引用时遇到问题.这样做的原因是我有一个Simulation实例,该实例被传递给模拟中的其他实例(例如Apple),因此它们可以自己修改模拟,甚至将自己从模拟中移除。在我更复杂的代码中,我得到了一个doublefree错误(当程序存在时),据我所知fromhere我不应该在同一个原始对象上创建两次shared_ptr。当模拟类不知道this时,如何将this作为shared_ptr传递给Apple对象已经是s

C++ this 和常量对象

你能告诉我为什么这段代码有效吗?replace_if算法使用了重载的operator()。在main函数中,我创建了IsEqual类的常量对象,因此只应使用常量函数成员。不知何故,恒定性不起作用,该运算符被调用。#include#include#includeclassIsEqual{intvalue;public:IsEqual(intv):value(v){}booloperator()(constint&elem){this->value=6;returnelem==value;}};intmain(){constIsEqualtst(2);std::vectorvec={3,2