I wanna make a wrapper for other class objs. When a wrapper obj is initialized, I want to be able to pass to its constructor the parameters I wanna pass to the inner obj:
template <class U, class... Args> struct T {
T(Args... args) {
data = new U(args...);
}
U* data;
};
I made a dummy Obj
:
struct Obj {
Obj(int a, int b) {
this->a = a;
this->b = b;
}
int a;
int b;
};
Now rather than using Obj obj(1, 2)
to initialize it, I wanna use the wrapper, as I would do some computing and management. So what I try to achieve is:
T<Obj> obj(1, 2); // doesn't work, this is what I want to achieve
T<Obj, int, int> obj(1, 2); // works, but it's not what I want it to look like
class... Args
should be a template parameter of the constructor, not of the entire class. Also you should use perfect forwarding here, even though it doesn't matter for struct Obj
.
template <class U>
struct T
{
template <class ...Args>
T(Args &&... args)
{
data = new U(std::forward<Args>(args)...);
}
U *data;
};