草庐IT

scope-identity

全部标签

c++ - "xvalue has identity"是什么意思?

C++11引入了新的值类别,其中之一是xvalue.是explained由Stroustrup描述为类似(im类别):“它是一个值,具有身份,但可以从中move”。另一个来源,cppreference解释:aglvalueisanexpressionwhoseevaluationdeterminestheidentityofanobject,bit-field,orfunction;和xvalue是glvalue,所以这句话对于xvalue是正确的也是。现在,我认为如果xvalue有身份,那么我可以检查两个xvalues指的是同一个对象,所以我取xvalue的地址.事实证明,这是不允许

c++ - thread_guard 与 scoped_thread

在书中"C++ConcurrencyInAction"byAnthonyWilliams您可以找到以下两段代码(我已经引入了一些小的修改):片段1:classthread_guard{std::thread&t;public:explicitthread_guard(std::thread&t_):t(t_){}~thread_guard(){if(t.joinable()){t.join();}}thread_guard(thread_guardconst&)=delete;thread_guard&operator=(thread_guardconst&)=delete;};voi

c++ - 使用自定义删除器 boost scoped_ptr/scoped_array

我不知道如何让scoped_ptr或scoped_array使用自定义删除器。也许还有另一种实现类似于shared_ptr允许受控删除?顺便说一句,为什么shared_ptr允许自定义删除器而scoped_ptr不允许?只是好奇。 最佳答案 Idon'tseehowtogetscoped_ptrorscoped_arraytousecustomdeleter你不能。Maybethereisanotherimplementationwhichallowscontrolleddeletionsimilartoshared_ptr?如果您

C++:引用 "out of scope"对象

关于引用文献,有一件事我一直不明白,我希望有人能帮助我。据我所知,引用不能为空。但是如果你有一个函数foo()返回对堆栈对象的引用会发生什么:Object&foo(){Objecto;returno;}Object&ref=foo();理论上ref将引用一个不存在的对象,因为一旦函数返回,o就会超出范围。这里发生了什么? 最佳答案 这会导致未定义的行为。不要这样做。在实现方面,实际上,引用将指向调用foo的堆栈框架所在的堆栈。在许多情况下,该内存仍然有意义,因此错误通常不会立即显现出来。因此,您应该注意永远不要创建这样的悬空引用。

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++ - 如何使用 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++ block scope extern declaration linkage,混淆C++标准解释

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

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)