Search code examples
c++boostc++14metaprogrammingboost-hana

How to use boost::hana::unpack with the constructor, instead of function?


Given this sample code, how to use pars to call a constructor to create a Foo2D object?

#include <boost/hana/tuple.hpp>
#include <boost/hana/unpack.hpp>

class Foo2D {
public:
    Foo2D(int x, int y):m_x(x), m_y(y) {}
private:
    int m_x;
    int m_y;
};

int main() {
    auto pars = boost::hana::make_tuple(10, 20);
    Foo2D foo1(10, 20); //my intention
    Foo2D foo2 = boost::hana::unpack(pars, Foo2D); //fails
}

Solution

  • Constructors are not function or functor. You might use lambda or regular functions:

    boost::hana::unpack(pars,
                        [](auto&&... args) {
                            return Foo2D(std::forward<decltype(args)>(args)...);
                        });