If you have an opaque pointer typedef, is there a way to dynamically refer to the pointed-to type, say, for use in templates? For instance, say you have something like this:
struct Foo; // Forward declared struct
typedef Foo* FooPtr; // Opaque pointer
Because the smart pointer types are templates in terms of the pointer-to type, to define a std::shared_ptr
of this, it seems that you have to say:
std::shared_ptr<struct Foo> theSharedPtr;
Is there any way to define such a pointer without "manually" unwrapping the opaque pointer typedef? I feel like I must be missing something obvious here, but you might imagine something like these (note: these do not work):
std::shared_ptr<*FooPtr> theSharedPointer;
// or
std::shared_ptr<pointedto(FooPtr)> theSharedPointer;
I feel like this should be possible. Am I missing something? I feel like this is an impending forehead-smacking moment...
EDIT: Noodling around some more, it appears that, in the common case, shared_ptr<T>
wants to take the sizeof(T)
. You can get around this by providing a deleter to the constructor. I suspect this makes this a bit of an edge case, but it still seems like with all the type wrangling in C++, I should be able to "unwrap" a pointer type without doing so by hand.
std::shared_ptr is a smart pointer that retains shared ownership of an object through a pointer.
The shared_ptr type is a smart pointer in the C++ standard library that is designed for scenarios in which more than one owner might have to manage the lifetime of the object in memory.
The smart pointer has an internal counter which is decreased each time that a std::shared_ptr , pointing to the same resource, goes out of scope – this technique is called reference counting. When the last shared pointer is destroyed, the counter goes to zero, and the memory is deallocated.
So the best way to return a shared_ptr is to simply return by value: shared_ptr<T> Foo() { return shared_ptr<T>(/* acquire something */); };
In C++11:
#include <type_traits>
typedef std::shared_ptr< std::remove_pointer< FooPtr >::type > theSharedPtr;
In C++03 you can use boost::remove_pointer
in the exact same way.
If you don't want to include boost, writing a remove_pointer
metafunction is quite easy:
template<class T> struct remove_pointer;
template<class T> struct remove_pointer<T*> { typedef T type; };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With