I need to implement a template function which would allow me to create an instance of arbitrary class using arbitrary constructor with any possible parameters which can be any combination of lvalues and rvalues.
Lets say I have two classes - A and B - which are as follows:
class A
{
public:
A(){}
};
class B
{
A& a1; // I want to be able to call non-constant methods and modify object
const A& a2; // This I need only as const member
int i; //can be initialized with a temporary object
public:
B(A& a1_, const A& a2_, int i_) : a(a_), a2(a2_), i(i_) {}
};
I tried to implement something like below but it would only allow me to use lvalues (named objects) and I will not be able to pass temporary objects. Adding const keyword partly solves the issue but disallows ability to modify objects which may be required.
template <typename TType, typename ... TArgs>
TType create(TArgs&... args)
{
return TType(args...);
}
I want to use 'create' function like this:
int main()
{
A a1;
A a2;
//function takes both lvalue and rvalue
B b = create<B>(a1, a2, 1);
}
Could someone please suggest a possible solution?