草庐IT

expected_conditions

全部标签

我的类中的 c++ condition_variable wait_for 谓词,std::thread <unresolved overloaded function type> error

我想在我的类中使用一个线程,然后该线程需要使用一个condition_variable,条件变量将被阻塞,直到一个谓词被更改为true。代码如下所示:classmyThreadClass{boolbFlag;threadt;mutexmtx;condition_variablecv;boolmyPredicate(){returnbFlag;}intmyThreadFunction(intarg){while(true){unique_locklck(mtx);if(cv.wait_for(lck,std::chrono::milliseconds(3000),myPredicate)

c++ - 错误 : expected unqualified-id before ‘.’ token//(struct)

我需要制作一个程序,从用户那里获取分数,然后对其进行简化。我知道该怎么做并且已经完成了大部分代码,但我一直收到此错误“错误:‘.’标记前应为不合格ID”。我已经声明了一个名为ReducedForm的结构,它包含简化的分子和分母,现在我要做的是将简化的值发送到这个结构。这是我的代码;在Rational.h中;#ifndefRATIONAL_H#defineRATIONAL_Husingnamespacestd;structReducedForm{intiSimplifiedNumerator;intiSimplifiedDenominator;};//Ihaveaclassherefor

c++ - [C++ 编译时断言] : Can we throw a compilation error if some condition is not met?

我写了一个函数:templatevoidtryHarder(){for(inti=0;i但我只希望它在N介于0和10之间时编译。我可以这样做吗?怎么办? 最佳答案 您可以使用static_assertdeclaration来完成:templatevoidtryHarder(){static_assert(N>=0&&N此功能仅在C++11之后可用。如果您坚持使用C++03,请查看Boost'sstaticassertmacro.整个想法都是很好的错误信息。如果您不关心这些,或者甚至负担不起boost,您可以执行以下操作:templa

c++ - "expected nested-name-specifier before ‘const’ 错误“在 g++ 中类型名称为 const

我在C++中有这段代码templateclassDD:publicenumerables{...private:typenameconstDD&mContainer;}它给了我两条错误信息:错误:在“const”之前需要嵌套名称说明符错误:“&”标记前的声明符无效typenameconst有什么问题?代码?它使用MSVCC++编译得很好。已添加typenameDD&constmContainer;和consttypenameDD&mContainer;给我同样的错误。 最佳答案 那么,typename在那里做什么?您指的不是嵌套类型

c++ - C++ 中的 "error: Expected a type, got ' 类名 '"

使用以下代码:templateclassnode{[...]};classb_graph{friendistream&operator>>(istream&in,b_graph&ingraph);friendostream&operatorvertices;//Thisline我得到:error:type/valuemismatchatargument1intemplateparameterlistfor‘templateclassstd::vector’error:expectedatype,got'node'error:templateargument2isinvalid在指示的行

C++ 模板 : conditionally enabled member function

我正在创建一个非常小的C++项目,我想创建一个简单的vector类来满足我自己的需要。std::vector模板类不会做。当vector类由char组成时s(即vector),我希望它能够与std::string进行比较.经过一番折腾之后,我编写了既能编译又能执行我想要的操作的代码。见下文:#include#include#includetemplateclassWorkingSimpleVector{public:constElementType*elements_;size_tcount_;//...templateinlinetypenamestd::enable_if::val

c++ - 是否可以实现 C++11 互斥锁概念以供 std::condition_variable 使用?

我发现std::mutexVisualStudio2013中的实现速度太慢。它使用一个重量级的互斥锁来确保即使在所有进程之间也可以实现同步,这一切都很好而且花花公子;除非你不与其他进程交谈并且真的可以使用CRITICAL_SECTION的额外速度在Win32上提供自旋锁。我试图实现fast_recursive_mutex遵循C++11互斥概念并根据规范履行所有义务。从任何意义上说,它都是std::mutex的直接替代品只要您不在进程之间进行同步。它与std::lock_guard配合得很好和std::unique_lock.但是我在尝试将它与std::condition_variabl

c++ - C++ 中的 If 语句,主体为空 : is condition guaranteed to be evaluated?

鉴于此声明(作为旁注,这不是我喜欢的编码风格)if(doSomething()){}“C++标准”是否保证函数被调用?(它的返回值对执行路径没有影响,所以编译器可能会遵循捷径评估的思想并将其优化掉。) 最佳答案 不涉及短路运算符,因此如果在不消除副作用的情况下无法优化函数,则可以保证调用该函数。引用C++11标准:[...]conformingimplementationsarerequiredtoemulate(only)theobservablebehavioroftheabstractmachineasexplainedbel

c++ - 为什么在通知 condition_variable 之前需要获取锁来修改共享的 "atomic"变量

这个问题在这里已经有了答案:Whyistherenowaitfunctionforcondition_variablewhichdoesnotrelockthemutex(1个回答)关闭7个月前。根据cppreference.com:Thethreadthatintendstomodifythevariablehastoacquireastd::mutex(typicallyviastd::lock_guard)performthemodificationwhilethelockisheldexecutenotify_oneornotify_allonthestd::condition

c++ - 将 std::condition_variable 与 atomic<bool> 一起使用

有几个关于SO处理原子的问题,以及其他处理std::condition_variable的问题。但是我的问题是我下面的用法是否正确?三个线程,一个ctrl线程在取消暂停其他两个线程之前做准备工作。当工作线程(发送者/接收者)处于紧密的发送/接收循环中时,ctrl线程还能够暂停它们。使用atomic的想法是在未设置暂停bool值的情况下使紧密循环更快。classSomeClass{public://...//Disregardthatdataispublic...std::condition_variablecv;//UDPthreadswillwaitonthiscvuntilallo