I am writing generic code, and I need to call the constructor of a generic template parameter T with a generic variadic tuple of arguments:
T& init_and_return(ArgsTuple& args)
{
m_data = std::apply(&T::T, args); // here compiler complains
return m_data;
}
In my main, T will be a type called A.
Compiler is saying "no member named T in A".
How can I refer to the constructor of T in a generic way?
The constructor is not a function or a method like other methods are -- it is special, and you cannot take its address. Personally I think it should be possible, but it isn't.
The C++ standard has make from tuple, which does what you want.
m_data = std::make_from_tuple<T>(args);
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