I\'m using a std::shared_ptr
in my application to make a smart pointer which can point to many different types of data structures like structs, vect
Can I cast the std::shared_ptr returned by find() back to std::shared_ptr? If so, how?
Yes. Use pointer casts. That said, in this case, the fact that you need to convert from void* to concrete types is most probably a symptom of poor design.
You should (almost) never need to convert from void* to strongly-typed pointer types ("almost" means I think you should never do it, but there may be some situations I have not considered).
More importantly, is this good practice?
No. As a rule of thumb, you should only convert to void* when you're interested in the value of the memory address, and not interested at all in the stored data.
Will this increase the complexity too much as the application scales?
The first problem I can think of, is that your types are no longer defined in a single place. This means, if you have pointer casts all over the place, when you decide that your class name changed, (or that you no longer use a std::string for a path but a boost::path object, and so on), you will have to go all over the code and update all the types in all the casts you added.
The second problem is, as the application scales, it will carry this casting problem around and propagate it in the code base, and make the situation worse for the entire code base. Your application code will become slightly (or not so slightly) less maintainable due to this. If you have other design compromises throughout the code, past a certain size, your application will become difficult/prohibitive to maintain.
Such an approach will simply make it impossible for you to write loosely-coupled code.
Or, is there some other completely different, elegant approach?
Go over your code and make a list of operations you use on the pointers. Then, add those operations as pure virtual functions in a base class. Implement a concrete template specialization of this base class, in terms of by the stored value type.
Code:
class pointer_holder { // get a better name
public:
virtual void teleport_pointer() = 0; // example operation
virtual ~pointer_holder() = 0;
};
template<typename T>
class type_holder: public pointer_holder {
public:
// TODO: add constructor and such
virtual void teleport_pointer() {
// implement teleport_pointer for T
}
virtual ~type_holder();
private:
T value_;
};
Client code:
std::unordered_map<std::string, std::shared_ptr<pointer_holder>> your_map;
your_map["int"] = new type_holder<int>{10};
your_map["string"] = new type_holder<std::string>{"10"};
your_map["int"]->teleport_pointer(); // means something different for each
// pointer type
If you store void*
, then at every use point you need to know the exact type you put in, as a cast-to-void*
-and-back is only valid if you go to/from the exact same type (not derived-to-void-to-base, for example).
Given that every use point will require knowing the type of the stored pointer, why round trip to void at all?
Have one map per type you store. This has modest overhead over one map per application (O(number of types) memory at run time, similar compile time).
With C++ template
s there will be little code duplication.
You can.
#include <memory>
#include <iostream>
#include <string>
using namespace std;
class wild_ptr {
shared_ptr<void> v;
public:
template <typename T>
void set(T v) {
this->v = make_shared<T>(v);
}
template <typename T>
T & ref() {
return (*(T*)v.get());
}
};
int main(int argc, char* argv[])
{
shared_ptr<void> a;
a = make_shared<int>(3);
cout << (*(int*)a.get()) << '\n';
cout << *static_pointer_cast<int>(a) << '\n'; // same as above
wild_ptr b;
cout << sizeof(b) << '\n';
b.set(3);
cout << b.ref<int>() << '\n';
b.ref<int>() = 4;
cout << b.ref<int>() << '\n';
b.set("foo");
cout << b.ref<char *>() << '\n';
b.set(string("bar"));
cout << b.ref<string>() << '\n';
return 0;
}
Output:
3
3
8
3
4
foo
bar
This is not good practice. If you don't store additional type information next to your std::shared_ptr
(which you can cast using static_pointer_cast) you have undefined behaviour all around. Maybe Boost.Any is an option for you?
If you want to stick with std::shared_ptr<void>
please remember to provide a custom deleter function (see make shared_ptr not use delete).