草庐IT

boolean_scope

全部标签

c++ - "Nested"scoped_lock

我缩短的简化类如下所示:classA{public://...methodA();methodB();protected:mutableboost::mutexm_mutex;sometype*m_myVar;}A::methodA(intsomeParam){boost::mutex::scoped_lockmyLock(m_mutex);m_myVar->doSomethingElse();}A::methodB(intsomeParam){boost::mutex::scoped_lockmyLock(m_mutex);m_myVar->doSomething();this->m

c++ - 可以在 C++ 中比较两个 boolean 值吗?

下面的代码应该可以工作吗?boolb1=true;boolb2=1我怀疑并非所有“真”都是平等的。 最佳答案 是的。所有真都是平等的。 关于c++-可以在C++中比较两个boolean值吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/2192253/

c++ - 如何使用 std::scoped_allocator_adapter?

据我了解,std::scoped_allocator_adapter提供一种控制机制,用于指定单独哪个分配器将由容器、其元素、其元素的元素等使用,假设元素本身是容器。也就是说,我无法理解std::scoped_allocator_adapter的语义.BjarneStroustrup在TheC++ProgrammingLanguage,section34.4.4,pg中提供了以下4个示例。1001(在接下来的问题中,我将它们称为Example-1、Example-2等。):Wehavefouralternativesforallocationofvectorsofstrings://v

c++ - 在计算中使用 boolean 值以避免分支

这是我想出的一些微优化的好奇心:structTimer{boolrunning{false};intticks{0};voidstep_versionOne(intmStepSize){if(running)ticks+=mStepSize;}voidstep_versionTwo(intmStepSize){ticks+=mStepSize*static_cast(running);}};这两种方法似乎实际上做同样的事情。第二个版本是否避免了分支(因此比第一个版本更快),或者是否有任何编译器能够使用-O3进行这种优化? 最佳答案

C++ block scope extern declaration linkage,混淆C++标准解释

标准N3242(C++11草案)和N3797(C++14draft)两者有相同的段落。§3.5Programandlinkage[basic.link]¶6Thenameofafunctiondeclaredinblockscopeandthenameofavariabledeclaredbyablockscopeexterndeclarationhavelinkage.Ifthereisavisibledeclarationofanentitywithlinkagehavingthesamenameandtype,ignoringentitiesdeclaredoutsidethei

c++ - 为什么不能将 C++ 设置迭代器强制转换为 boolean 值?

对于一个STL集,您似乎应该能够说:if(s.find(x)){//Something}相对于if(s.find(x)!=s.end()){//Something}此外,如果可以将set-iterators强制转换为bool(如果内部指针不为null,则为true),您就可以做到。为什么STL集合迭代器没有这个简单的功能?这是故意遗漏的吗?澄清:或者,set可以有一个直接返回bool的set::contains(x)方法,但这似乎也没有实现。我知道它只有几个字符,但在s是某个函数的返回值的情况下,这可能会令人沮丧,因为需要创建一个临时变量,即(假设m的类型为map>)constset&

c++ - 错误 : uint64_t was not declared in this scope when compiling C++ program

我正在尝试一个简单的程序来打印steady_clock的时间戳值,如下所示:#include#includeusingnamespacestd;intmain(){cout(steady_clock::now().time_since_epoch()).count();cout但是每当我像这样编译时g++-oabcabc.cpp,我总是会遇到错误:Infileincludedfrom/usr/include/c++/4.6/chrono:35:0,fromabc.cpp:2:/usr/include/c++/4.6/bits/c++0x_warning.h:32:2:error:#er

c++ - 非本地 lambda 和捕获变量 - "block scope"是什么意思

我目前正在玩c++11lambda,发现了一个我无法理解的例子。根据标准:Alambda-expressionwhosesmallestenclosingscopeisablockscope(3.3.3)isalocallambdaexpression;anyotherlambda-expressionshallnothaveacapture-listinitslambda-introducer所以,我创建了一个简单的例子:inta=10;autox=[a]{return1;};intmain(){intk=5;autop=[k]{returnk;};return0;}ideone中的

C++:为什么这个简单的 Scope Guard 有效?

到目前为止,每个看过的作用域守卫都有一个守卫bool变量。例如,请参阅此讨论:Thesimplestandneatestc++11ScopeGuard但是一个简单的守卫可以工作(gcc4.9,clang3.6.0):templatestructfinally_t:publicC{finally_t(C&&c):C(c){}~finally_t(){(*this)();}};templatestaticfinally_tfinally_create(C&&c){returnstd::forward(c);}#defineFINCAT_(a,b)a##b#defineFINCAT(a,b)

c++ - C++ 11 中 boost::scoped_ptr 的替代方案

我们刚刚将编译器升级到支持C++11的VC++2013。之前我们一直在使用来自Boost的shared_ptr和scoped_ptr类,但由于这是我们一直在使用的Boost类,我们正在寻找删除该依赖项。据我所知,std::shared_ptrs是boost::shared_ptrs的直接替代品,所以这(希望)很容易。但是,Boostscoped_ptrs的最佳替代品是什么(如果有的话)?会是unique_ptr吗?(老实说,虽然我写了代码,但那是大约10年前的事了,我已经忘记了使用scoped_ptrs的目的是什么......也许我只是在“玩”Boost,但到目前为止正如我所看到的,在