C++ obtaining the type of a constructor

There is no way to refer to a constructor as a function. The standard very explicitly states that constructors have no names. You can't take the address of a constructor.

An alternative might be to require of any type to be used with some machinery, that it has an associated traits type that provides tuples or something corresponding to the constructors.

Before we got language support for decltype as I recall the Boost functionality for finding the result type of a function relied on a registration scheme for possible types.


There is a solution that allows you to obtain constructor parameters types.

Note: it finds the first ctor having unambiguous and shortest set of parameters.

Look at my example here: https://godbolt.org/z/FxPDgU

In your example, the statement refl::as_tuple<foo> will result in std::tuple<int, double>. Once you have this tuple type, you can whatever you want, including foo type instantiation.

The code above is based on a solution for determining types used for aggregate-init extended to handle user-defined ctors.

Related materials:

  1. http://alexpolt.github.io/type-loophole.html

    https://github.com/alexpolt/luple/blob/master/type-loophole.h

    by Alexandr Poltavsky, http://alexpolt.github.io

  2. https://www.youtube.com/watch?v=UlNUNxLtBI0

    Better C++14 reflections - Antony Polukhin - Meeting C++ 2018