Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::apply to constructor in a templated function [duplicate]

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?

like image 249
nyarlathotep108 Avatar asked Aug 27 '26 19:08

nyarlathotep108


1 Answers

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);
like image 81
Yakk - Adam Nevraumont Avatar answered Aug 29 '26 08:08

Yakk - Adam Nevraumont