草庐IT

my_shared_ptr

全部标签

c++ - std::shared_timed_mutex 上的共享锁可以升级为独占锁吗?

新的std::shared_timed_mutex允许两种类型的锁:共享锁和排他锁。如果一个人持有共享锁,有没有办法将它原子交换(“升级”)为独占锁?换句话说,给定以下代码,我怎样才能避免非原子丢弃和重新锁定?std::shared_timed_mutexm;//Guardsastd::vector.m.lock_shared();//Readfromvector.(Sharedlockissufficient.)//...//Nowwewanttowritetothevector.Weneedanexclusivelock.m.unlock_shared();//理想情况下,m.un

c++ - std::make_shared() 是否使用自定义分配器?

考虑thiscode:#include#includeclassSomeClass{public:SomeClass(){std::coutptr1(newSomeClass);std::coutptr2(std::make_shared());std::cout这是它的输出:CustomnewSomeClass()Anotherone...SomeClass()Done!~SomeClass()~SomeClass()Customdelete显然,std::make_shared()没有调用new运算符——它使用的是自定义分配器。这是std::make_shared()的标准行为吗?

c++ - std::make_shared() 是否使用自定义分配器?

考虑thiscode:#include#includeclassSomeClass{public:SomeClass(){std::coutptr1(newSomeClass);std::coutptr2(std::make_shared());std::cout这是它的输出:CustomnewSomeClass()Anotherone...SomeClass()Done!~SomeClass()~SomeClass()Customdelete显然,std::make_shared()没有调用new运算符——它使用的是自定义分配器。这是std::make_shared()的标准行为吗?

c++ - 如何将 shared_ptr 与指向不应释放的结构的指针一起使用

目前我正在使用glib库中的一些函数。伴随着glib的还有gio。glib是一个C库,因此我需要删除我创建的一些结构。我为许多对象创建了一个智能指针,例如:std::shared_ptrmy_queue=std::shared_ptr(g_async_queue_create(),g_async_queue_unref);为此创建了一个指向GAsyncQueue的共享指针,这会在队列生命结束时安全地销毁队列。但是,当我从gio库中获得一个我不应该释放的指针时,我遇到了一个问题。在下面的代码中,my_connection是一个GSocketClient,它实现(在glib中)GIOStr

c++ - 如何将 shared_ptr 与指向不应释放的结构的指针一起使用

目前我正在使用glib库中的一些函数。伴随着glib的还有gio。glib是一个C库,因此我需要删除我创建的一些结构。我为许多对象创建了一个智能指针,例如:std::shared_ptrmy_queue=std::shared_ptr(g_async_queue_create(),g_async_queue_unref);为此创建了一个指向GAsyncQueue的共享指针,这会在队列生命结束时安全地销毁队列。但是,当我从gio库中获得一个我不应该释放的指针时,我遇到了一个问题。在下面的代码中,my_connection是一个GSocketClient,它实现(在glib中)GIOStr

c++ - 为什么我不能将 nullptr 转换为 weak_ptr<>

classMyClass{public:MyClass(std::weak_ptrparent){}}我想这样做:autonewInstance=std::make_shared(nullptr);或者weak_ptr参数的默认值为null,如:voidfunction(intarg,std::weak_ptrobj=nullptr);但是,我需要这样做:autonewInstance=std::make_shared(std::shared_ptr(nullptr));这是为什么呢? 最佳答案 因为weak_ptr在概念上只能从另

c++ - 为什么我不能将 nullptr 转换为 weak_ptr<>

classMyClass{public:MyClass(std::weak_ptrparent){}}我想这样做:autonewInstance=std::make_shared(nullptr);或者weak_ptr参数的默认值为null,如:voidfunction(intarg,std::weak_ptrobj=nullptr);但是,我需要这样做:autonewInstance=std::make_shared(std::shared_ptr(nullptr));这是为什么呢? 最佳答案 因为weak_ptr在概念上只能从另

c++ - clang 3.1 看不到 unique_ptr?

我刚刚开始玩clang并尝试编译以下示例程序:#include#includeintmain(){std::unique_ptru(newunsigned(10));std::cout编译时出现以下错误:$clang++helloworld.cpphelloworld.cpp:6:10:error:nomembernamed'unique_ptr'innamespace'std'std::unique_ptru(newunsigned(10));~~~~~^helloworld.cpp:6:29:error:expected'('forfunction-stylecastortypec

c++ - clang 3.1 看不到 unique_ptr?

我刚刚开始玩clang并尝试编译以下示例程序:#include#includeintmain(){std::unique_ptru(newunsigned(10));std::cout编译时出现以下错误:$clang++helloworld.cpphelloworld.cpp:6:10:error:nomembernamed'unique_ptr'innamespace'std'std::unique_ptru(newunsigned(10));~~~~~^helloworld.cpp:6:29:error:expected'('forfunction-stylecastortypec

c++ - 为什么 unique_ptr::reset 没有带删除器的重载?

unique_ptr::reset没有使用constdeleter&和deleter&&来匹配其构造函数的重载是否有原因?那些作为第二个论点?unique_ptr中存储的删除器将使用来自reset的参数进行复制分配或移动分配。如果删除器不可复制或不可移动,则调用reset的相应重载将无法编译。这似乎与构造函数的行为一致。 最佳答案 我考虑过添加它,但您可以使用移动赋值运算符获得等效功能:ptr=unique_ptr(newT(another_value),D(another_state));所以我选择不使用reset说同样的话,以保