smart-pointers

Using std::shared_ptr<void> to point to anything

泄露秘密 提交于 2019-11-28 01:12:46
问题 I'm using a std::shared_ptr<void> in my application to make a smart pointer which can point to many different types of data structures like structs, vectors, matrices... basically anything. What I'm trying to do is map some names to their data structures. I'm performing the mapping using a hashtable: std::unordered_map<std::string, std::shared_ptr<void>> Can I cast the std::shared_ptr<void> returned by find() back to std::shared_ptr<my_type> ? If so, how? More importantly, is this good

Why is std::rc::Rc<> not Copy?

眉间皱痕 提交于 2019-11-27 17:22:44
问题 Can someone explain to me why Rc<> is not Copy ? I'm writing code that uses a lot of shared pointers, and having to type .clone() all the time is getting on my nerves. It seems to me that Rc<> should just consist of a pointer, which is a fixed size, so the type itself should be Sized and hence Copy , right? Am I missing something? 回答1: It seems to me that Rc<> should just consist of a pointer, which is a fixed size, so the type itself should be Sized and hence Copy , right? This is not quite

Can storing unrelated data in the least-significant-bit of a pointer work reliably?

╄→尐↘猪︶ㄣ 提交于 2019-11-27 15:06:27
问题 Let me just say up front that what I'm aware that what I'm about to propose is a mortal sin, and that I will probably burn in Programming Hell for even considering it. That said, I'm still interested in knowing if there's any reason why this wouldn't work. The situation is: I have a reference-counting smart-pointer class that I use everywhere. It currently looks something like this (note: incomplete/simplified psuedocode): class IRefCountable { public: IRefCountable() : _refCount(0) {}

How could one implement std::auto_ptr's copy constructor?

这一生的挚爱 提交于 2019-11-27 14:51:04
Back on my crazy AutoArray thingy ... (quoting important bits from there: class AutoArray { void * buffer; public: //Creates a new empty AutoArray AutoArray(); //std::auto_ptr copy semantics AutoArray(AutoArray&); //Note it can't be const because the "other" reference //is null'd on copy... AutoArray& operator=(AutoArray); ~AutoArray(); //Nothrow swap // Note: At the moment this method is not thread safe. void Swap(AutoArray&); }; ) Anyway, trying to implement the copy constructor. There's a piece of client code (not yet committed into bitbucket because it won't build) that looks like this:

smart pointers not working with Android NDK r8

旧时模样 提交于 2019-11-27 14:48:58
问题 I can't figure out how to use shared pointers within my Android project. I'm using the latest Eclipse ADT on Mac OS X with the Android NDK r8d. Here is what is in my Android.mk file: LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_CPPFLAGS := -std=c++11 LOCAL_MODULE := native LOCAL_SRC_FILES := native.cpp include $(BUILD_SHARED_LIBRARY) Here is what is in my Application.mk file: NDK_TOOLCHAIN_VERSION=4.7 APP_STL := stlport_shared I've tried the default GCC 4.6, the experimental 4.7,

Propagate constness to data pointed by member variables

允我心安 提交于 2019-11-27 13:49:15
问题 It is often quite confusing to C++ newcomers that const member functions are allowed to call non-const methods on objects referenced by the class (either by pointer or reference). For example, the following is perfectly correct: class SomeClass { class SomeClassImpl; SomeClassImpl * impl_; // PImpl idiom public: void const_method() const; }; struct SomeClass::SomeClassImpl { void non_const_method() { /*modify data*/ } }; void SomeClass::const_method() const { impl_->non_const_method(); //ok

What happens when using make_shared

随声附和 提交于 2019-11-27 12:07:37
问题 I'm interested if these two lines of code are the same: shared_ptr<int> sp(new int(1)); // double allocation? shared_ptr<int> sp(make_shared<int>(1)); // just one allocation? If this is true could someone please explain why is it only one allocation in the second line? 回答1: The first case does not perform a double allocation, it performs two allocations, one for the managed object and one for the control block of the shared_ptr . For the second case, cppreference has a good explanation for

Deletion of pointer to incomplete type and smart pointers

微笑、不失礼 提交于 2019-11-27 11:15:48
问题 When trying to use an auto_ptr with a type that was declared with forward-declaration, like this: class A; ... std::auto_ptr<A> a; the destructor of A is not called (apparently, because auto_ptr internally delete s the underlying pointer and the destructor for an incomplete type cannot be called). However, the same code works fine and the destructor is called when using std::shared_ptr instead of std::auto_ptr . How can that be explained? 回答1: A shared_ptr can be declared with an incomplete

C++11: Replace all non-owning raw pointers with std::shared_ptr()?

放肆的年华 提交于 2019-11-27 10:27:39
With the advent of std::unique_ptr , the blemished std::auto_ptr can finally be put to rest. So for the last several days, I have been changing my code to use smart pointers and to eliminate all delete from my code. Although valgrind says my code is memory-clean, the semantic richness of smart pointers will make for cleaner and easier-to-understand code. In most of the code, the translation is simple: use std::unique_ptr for in place of the raw pointers held by the owning objects, throw out delete , and carefully sprinkle get() , reset() and move() calls, as needed, to interface well with the

Example to use shared_ptr?

我的未来我决定 提交于 2019-11-27 10:13:17
Hi I asked a question today about How to insert different types of objects in the same vector array and my code in that question was gate* G[1000]; G[0] = new ANDgate() ; G[1] = new ORgate; //gate is a class inherited by ANDgate and ORgate classes class gate { ..... ...... virtual void Run() { //A virtual function } }; class ANDgate :public gate {..... ....... void Run() { //AND version of Run } }; class ORgate :public gate {..... ....... void Run() { //OR version of Run } }; //Running the simulator using overloading concept for(...;...;..) { G[i]->Run() ; //will run perfectly the right Run