5

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?

nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64

1 Answers1

15

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);
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • Could you please explain how constructor is not a function or method? – SolidMercury Jul 04 '19 at 17:03
  • `[class.mem.special]` states that "*The default constructor, copy constructor, move constructor, copy assignment operator, move assignment operator, and destructor are **special member functions**."*. That kinda contradicts with what you said, doesn't it? – Fureeish Jul 04 '19 at 17:06
  • 3
    @Fureeish Sure, that is technically correct, which is the best kind of correct. – Yakk - Adam Nevraumont Jul 04 '19 at 17:25