I have a problem in making a static_cast
of an unique_ptr
void *
by passing arguments to the constructor. In particular, what you see in the object3
.
It is important that the solution is at compile time, and the type must be the same for smart pointers as for stacked objects.
Any solution?
class Test {
public:
Test(){}
Test(int n) : num(n) {}
int num;
};
template<typename T>
class Object {
public:
Object(T&& v) : value(static_cast<T*>(std::move(&v))) {}
Object(const T& v) : value(static_cast<T*>(&v)) {}
Object(std::unique_ptr<T>&& v) : value(static_cast<std::unique_ptr<T>*>(std::move(&v))) {}
Object(const std::unique_ptr<T>& v) : value(static_cast<std::unique_ptr<T>*>(&v)) {}
T* operator->() { return static_cast<T*>(value); }
private:
void* value;
};
int main(int argc, char *argv[]) {
Object<Test> object1 = Test(1);
cout << object1->num << endl; // print 1
Object<Test> object2 = Test();
object2->num = 2;
cout << object2->num << endl; // print 2
Object<Test> object3 = std::make_unique<Test>(3);
cout << object3->num << endl; // print 0 ¿?¿?¿?
Object<Test> object4 = std::make_unique<Test>();
object4->num = 4;
cout << object4->num << endl; // print 4
return 0;
}
result:
1
2
0
4