草庐IT

this_record

全部标签

c++ - std::condition_variable::wait_until 相对于 std::this_thread::sleep_for 有什么优势吗?

在时间等待场景中:oursoftwareworksinthebackground,andsynchronizesdatawiththeserverinevery20-30minutes.我想用std::this_thread::sleep_for但我的上级强烈反对任何形式的sleep功能。他推荐std::condition_variable::wait_until(lock,timeout-time,pred)不知道在这种情况下sleep_for有什么缺点吗? 最佳答案 正如评论中已经指出的那样,这仅取决于您的用例。两者之间的主要区

c++ - this指针是如何捕获的?

考虑以下代码:structS{intx;voidf(){autol=[&](){x=42;};//thisisimplicitlycapturedhere}};§5.1.2/14指出:Anentityiscapturedbycopyifitisimplicitlycapturedandthecapture-defaultis=orifitisexplicitlycapturedwithacapturethatdoesnotincludean&.因此我得出结论,this不是通过复制捕获。但随后通过§5.1.2/15:Anentityiscapturedbyreferenceifitisi

c++ - 如何在没有多余 RC 操作的情况下在派生类中使用 shared_from_this?

如果我想创建一个shared_ptr在从基类继承的层次结构中的派生类成员函数中,我可以使用shared_from_this和static_pointer_cast:classBase:publicstd::enable_shared_from_this{};classDer:publicBase{public:std::shared_ptrmake_SP_to_Me(){returnstd::static_pointer_cast(shared_from_this());}};我担心的是static_pointer_cast通过lvalue-ref-to-const接受它的参数,所以当

c++ - 在括号内的 lambda 中声明名称为 `this` 的变量会在 3 个不同的编译器上产生不同的结果

在C++中,可以在括号内声明变量,例如int(x)=0;。但似乎如果您使用this而不是变量名,则使用构造函数代替:A(this);调用A::A(B*)。那么第一个问题就是为什么this不一样,是不是因为变量不能命名为this?让事情变得复杂一点,让我们把this放在lambda中-structB;structA{A(B*){}};structB{B(){[this]{A(this);}();}};现在gcc调用A::A(B*),msvc打印关于缺少默认构造函数的错误,clang打印expectedexpression(https://godbolt.org/g/Vxe0fF)。它在m

c++ - 关键字 `this` 可以在类范围内使用吗?

看来我可以在类范围内定义捕获this的lambda表达式。据我阅读N4640最新的工作草案,我找不到允许该行为的句子。我想我错过了什么......这是一个例子:#include#includestructfoo{std::functionf1=[this]{++i;};inti=0;};intmain(){fooa;fooconst&cref=a;cref.f1();std::cout运行演示。(g++-std=c++11迂腐)https://wandbox.org/permlink/HPzaOxbBkOQOmuS6已更新感谢@Brian和@cpplerner的评论,我明白了我的基本问

c++ - 来自同一个 enable_shared_from_this 实例的两个 shared_ptr

鉴于这个类是enable_shared_from_this:classconnection:publicstd::enable_shared_from_this{//...};假设我从sameconnection*创建了两个std::shared_ptr实例,如下所示:std::shared_ptrrc(newconnection);std::shared_ptrfc(rc.get(),[](connectionconst*c){std::cout到目前为止一切正常,因为资源{connection*}由单个shared_ptr—rc准确地说,fc只是有一个假的删除器。之后,我这样做:a

c++ - GCC 内联汇编错误 : Cannot take the address of 'this' , 这是一个右值表达式

我仍在与GCC作斗争-编译以下内联汇编代码(使用-fasm-blocks,启用Intel风格汇编syntax)给我一个奇怪的错误Cannottaketheaddressof'this',whichisanrvalueexpression...MyClass::MyFunction(){_asm{//...movebx,this//error:Cannottaketheaddressof'this',whichisanrvalueexpression//...moveax,this//error:Cannottaketheaddressof'this',whichisanrvalueex

c++ - 替代 std::this_thread::sleep_for()

我有一个循环,我想确保它在每个循环中运行(大约)固定的时间量。我正在使用sleep_for来实现此行为,但我也希望程序能够在不完全支持标准线程库的环境中编译。现在我有这样的东西:usingnamespacestd;usingnamespacestd::chrono;//while(!quit){steady_clock::time_pointthen=steady_clock::now();//...doloopstuffsteady_clock::time_pointnow=steady_clock::now();#ifdefNOTHREADS//versionforsystemsw

c++ - decltype(*this) 等效于外部函数体

是否可以编写一个trait,它会导致使用它的类的类型?如何在下面的示例中实现get_class?classFoo{typedefget_class::typetype;//type=Foonow};注意:我必须写一个宏,它在类主体中扩展,用于多个类,所以我不能简单地写'typedefFootype;'用例:我有一个reflectable(...)宏,它生成基础设施来遍历成员,访问他们并使用他们的名字查找他们:classFoo{friendstd::ostream&operatorreflectable(...)应该是一个宏,这样我就可以将类型和成员名称作为字符串分别获取,以构建用于查找

c++ - MPL 序列 : is this legal? 的用户定义文字

能够将传递给文字运算符的字符串转换为MPL序列会很有用,因为这样我们就可以根据字符串的内容控制代码生成。以前,我认为这是不可能的,因为constexpr函数的参数在函数体内不被视为常量表达式。但是,我想出了以下在Clang3.4.2和GCC4.8.2下编译的解决方法:#include#include#includestructstring{constuintmax_tm_str[64];constsize_tm_length;templateconstexprstring(constTs...ts):m_str{(uintmax_t)ts...},m_length{sizeof...(