草庐IT

THIS_ARCH

全部标签

【C++初阶】三、类和对象(面向过程、class类、类的访问限定符和封装、类的实例化、类对象模型、this指针)

=========================================================================相关代码gitee自取:C语言学习日记:加油努力(gitee.com) =========================================================================接上期:【C++初阶】二、入门知识讲解(引用、内联函数、auto关键字、基于范围的for循环、指针空值nullptr)-CSDN博客 ===================================================

c++ - 为什么我无法通过 lambda 捕获 "this"指针?

考虑以下代码:classA{public:voidfoo(){autofunctor=[this](){A*a=this;autofunctor=[a]()//Thecompilerwon'taccept"this"insteadof"a"{a->bar();};};}voidbar(){}};在VC2010中,使用this代替a会导致编译错误。其中:1>main.cpp(20):errorC3480:'`anonymous-namespace'::::__this':alambdacapturevariablemustbefromanenclosingfunctionscope1>m

c++ - std::this_thread::sleep_for() 可以有虚假唤醒吗?

请注意,这不是关于std::condition_variable::wait_for()的问题。我知道这可能会虚假唤醒。我的程序的行为表明这个问题的答案是肯定的,但是STL文档对于condition_variable的情况非常清楚。至少在cppreference.com,this_thread的正确答案似乎是否。编译器是gcc4.8.1,以防这是一个缺陷。 最佳答案 C++标准的相关部分(第[thread.thread.this]/7-9段)没有提及任何关于std::this_thread::sleep_for的虚假唤醒,不像例如对

c++ - 在构造函数中指向 this 的弱指针

我知道无法通过从类的构造函数调用shared_from_this()来获取shared_ptr,因为该对象尚未构造。但是,是否有可能从构造函数中获得对象的weak_ptr?一些讨论“weak_from_raw()”方法的boost论坛帖子表明这是可能的。编辑:Boost形式讨论weak_from_rawhttp://lists.boost.org/boost-users/2010/08/61541.php 最佳答案 我想你指的是什么isthis.这似乎没有被合并到boost版本中(这可能是错误的)。来自boostdocs:常见问题问

c++ - gcc 与 clang、msvc 和 icc : Is this function call ambiguous?

我能得到的所有编译器都同意这很好:templateautofoo(Check,T...)->void;templateautofoo(int,T...)->void;intmain(){foo(7,"");}但是,根据gcc,以下代码(带有不能从函数参数推导的前导模板参数)是不明确的:templateautobar(Check,T...)->void;templateautobar(int,T...)->void;intmain(){bar(7,"");//ambiguousaccordingtogccbar(7);//justfine}另一方面,clang、msvc和icc对此非常满

move 操作后 C++ lambda ‘this’ 指针失效

我当前的项目中有以下(简化的)代码:#include#include#include#includeclassTest{public:Test()=default;Test(constTest&other)=delete;Test&operator=(constTest&other)=delete;Test(Test&&other)=default;Test&operator=(Test&&other)=default;voidsetFunction(){lambda=[this](){a=2;};}intcallAndReturn(){lambda();returna;}privat

c++ - 直接调用析构函数后 `new (this) MyClass();`是不是未定义行为?

在thisquestionofmine,@DeadMG说通过this指针重新初始化一个类是未定义的行为。标准中有没有提到它?例子:#includeclassX{int_i;public:X():_i(0){std::cout~X();new(this)X(5);}voidprint_i(){std::coutExampleoutputatIdeone(我知道UB也可以是“看似正确的行为”)。请注意,我没有在类外部调用析构函数,因为不访问生命周期已结束的对象。另请注意,@DeadMG说直接调用析构函数是可以的,只要它对每个构造函数调用一次即可。 最佳答案

c++ - noexcept 规范中是否允许使用 `this`?

我有一些代码要求我使用*this,但我希望它是noexcept友好的:structfoo;//Wouldactuallybesomethingwithconditionalnoexceptvoiddo_something(foo&);structfoo{voidfn()noexcept(noexcept(::do_something(*this))){::do_something(*this);}};然而,gccrejectsthis::7:43:error:invaliduseof'this'attoplevelnoexcept(noexcept(::do_something(*th

c++ - enable_shared_from_this 不适用于 xcode 5

#include#includetemplateclassTest:publicstd::enable_shared_from_this>{public:std::shared_ptr>getMe(){returnshared_from_this();};};intmain(intargc,constchar*argv[]){TestaTest;return0;}当我尝试在Xcode5上编译它时,我得到了Useofundeclaredidentifier'shared_from_this'我测试了它并在VisualStudio2010上运行。 最佳答案

The MySQL server is running with the LOCK_WRITE_GROWTH option so it cannot execute this statement

今天发现mysql报错,记录下问题原因;错误信息:TheMySQLserverisrunningwiththeLOCK_WRITE_GROWTHoptionsoitcannotexecutethisstatement向aliyun写入数据,报错。阿里云的一个保护策略,空间剩余不足时,禁止数据写入;可用navicat执行以下sql查看剩余空间大小;SELECTTABLE_SCHEMA,concat(TRUNCATE(sum(data_length)/1024/1024,2),‘MB’)ASdata_size,concat(TRUNCATE(sum(index_length)/1024/1024,