草庐IT

strict-weak-ordering

全部标签

c++ - 在库接口(interface)中提供对 weak_ptr 的访问是否明智?

我编写了一个库,公开了对多个相关对象类型的引用。所有这些对象的生命周期都由库在内部通过boost::shared_ptr管理。根据库的性质,库的用户还可以知道任何公开对象的生命周期。所以他们可以存储指针或保留对这些对象的引用。他们这样做并知道这些对象何时不再有效是合理的。但我对强制我的用户讲道理感到内疚。公开库是否可以接受weak_ptr的对象?其他图书馆有这样做吗?我已经分析了这个库在应用程序中的使用情况,发现它对任务至关重要,无法公开weak_ptr独家。让匹配的API函数公开一个引用或一个weak_ptr或使任何对象能够公开一个weak_ptr是否更明智?对自己?

C++ strict-aliasing agnostic cast

我在StackOverflow中阅读了很多关于严格别名的QA,但它们都很常见,而且讨论总是倾向于引用C++标准的深层细节,这些细节几乎总是难以正确理解。特别是当标准不直接说,而是用一种含糊不清的方式描述某些东西时。所以,我的问题可能与这里的大量QA重复,但是请只回答一个具体问题:做一个“nonalias_cast”是正确的方法吗?:templateinlineautononalias_cast(IN*data){char*tmp=reinterpret_cast(data);returnreinterpret_cast(tmp);}floatf=3.14;unsigned*u=nona

c++ - 为什么我们需要 C++11 中的 weak_ptr?

我正在阅读NicolaiM.Josuttis的“TheC++StandardLibrary”一书以了解弱指针。作者提到了需要weak_ptr的两个原因,我不明白第二个原因。任何人都可以提供一个简单的解释以及以下原因的示例(引自书中):Anotherexampleoccurswhenyouexplicitlywanttosharebutnotownanobject.Thus,youhavethesemanticsthatthelifetimeofareferencetoanobjectoutlivestheobjectitrefersto.Here,shared_ptrswouldnev

c++ - 为什么宏 __STL_FUNCTION_TMPL_PARTIAL_ORDER 应该将模板函数包含在 std_pair.h 中

今天在STL_pair.h中看到如下代码:#ifdef__STL_FUNCTION_TMPL_PARTIAL_ORDERtemplateinlinebooloperator!=(constpair&__x,constpair&__y){return!(__x==__y);}templateinlinebooloperator>(constpair&__x,constpair&__y){return__y我不认为模板函数与偏特化有任何关联的功能模板。我错了吗? 最佳答案 编译器如何处理函数调用在C++中调用函数模板经历了名称查找(标准

c++ - 使用 std::weak_ptr 和别名构造函数打破循环引用:听起来还是有问题?

我还没有在任何主要的C++论坛/博客(例如GotW)上找到以下打破循环引用的方法,所以我想问一下该技术是否已知,其优缺点是什么?classNode:publicstd::enable_shared_from_this{public:std::shared_ptrgetParent(){returnparent.lock();}//thegetterfunctionsensurethat"parent"alwaysstaysalive!std::shared_ptrgetLeft(){returnstd::shared_ptr(shared_from_this(),left.get())

C++0x : memory ordering

当前C++0xdraft在第29.3.9节和第29.3.10节第1111-1112页中说明,在以下示例中://Thread1r1=y.load(memory_order_relaxed);x.store(1,memory_order_relaxed);//Thread2r2=x.load(memory_order_relaxed);y.store(1,memory_order_relaxed);结果r1=r2=1是可能的,因为每个线程的操作都放宽了并且指向不相关的地址。现在我的问题是关于以下(类似)示例的可能结果://Thread1r1=y.load(memory_order_acqu

c++ - GCC 7,aligned_storage 和 "dereferencing type-punned pointer will break strict-aliasing rules"

我编写的代码在GCC4.9、GCC5和GCC6中没有警告。它在一些较旧的GCC7实验快照(例如7-20170409)中也没有警告。但在最近的快照(包括第一个RC)中,它开始产生关于别名的警告。代码基本上可以归结为:#includestd::aligned_storage::typestorage;intmain(){*reinterpret_cast(&storage)=42;}使用最新的GCC7RC编译:$g++-Wall-O2-cmain.cppmain.cpp:Infunction'intmain()':main.cpp:7:34:warning:dereferencingtyp

C++ weak_ptr.lock() 段错误

所以我有这个函数来添加监听器,它转换一个类的共享指针,以便我稍后在收到通知时可以调用它。voidregisterListener(std::shared_ptrlistener){if(!listener){qCWarning(OBSERVER_LOGGER)lock()){if(listener==shared){return;}iter++;}else{iter=listeners.erase(iter);}}autoweak=std::weak_ptr(listener);listeners.push_back(weak);}voidnotify(std::function)>o

c++ - 即使在 std::shared_ptr 拥有之后,shared_from_this 还是空的 _M_weak_this

我在A中存储了一个类(我们称它为std::vector)使用C++智能指针(因此vector签名为std::vector>)。#include#include#includeclassA:std::enable_shared_from_this{public:voiddoWork();std::shared_ptrgetSharedRef();};voidA::doWork(){std::coutA::getSharedRef(){returnshared_from_this();}classAManager{staticstd::vector>aList;public:staticv

c++ - std::memory_order_relaxed 相对于相同原子变量的原子性

关于内存顺序的cppreference文档说Typicaluseforrelaxedmemoryorderingisincrementingcounters,suchasthereferencecountersofstd::shared_ptr,sincethisonlyrequiresatomicity,butnotorderingorsynchronization(notethatdecrementingtheshared_ptrcountersrequiresacquire-releasesynchronizationwiththedestructor)这是否意味着宽松的内存排序