I'm interested in doing something along the lines of:
lazy_adaptor lz(arg1, arg2, arg3, ...)
where then lz.construct() then constructs an object of obj_type with ctor
of arguments "arg1, arg2, arg3, ...". So far I have the following: (Am I
on the right track?)
#include <utility>
#include <tuple>
#include <string>
#include
template
struct ctor_forwarder;
template
ctor_forwarder
make_ctor_forwarder(Args&&... args)
{
// Note: The "Args..." template parameter to ctor_forwarder determines
how
// the caller's arguments are temporarily stored. If it's an rvalue,
it's
// temporarily stored by value, else it's temporary stored by
reference.
return
ctor_forwarder(std::forward<Args>(args)...);
}
template
struct ctor_forwarder
{
//TODO: make ctor private.
template
explicit ctor_forwarder(A&&... args)
: m_args( ::std::forward<A>(args)... )
{}
/*
template <typename Callable>
auto forward_to(Callable && caller) ->
decltype( caller(::std::forward<Args>(m_args)...) )
{
return caller(::std::forward<Args>(m_args)...);
}
*/
template <typename T>
void forward_to(::boost::optional<T> & obj)
{
obj.emplace(::std::forward<Args>(m_args)...);
}
private:
typedef ::std::tuple args_t;
private:
args_t m_args;
};
template
struct lazy_adaptor
{
Obj on_enter()
{
m_args_forwarder.forward_to(m_obj);
}
private:
::boost::optional<Obj> m_obj;
ctor_forwarder m_args_forwarder;
};
template
lazy_adaptor make_lazy_adaptor(Args&&... args)
{
return lazy_adaptor();
}
struct Sample1
{
Sample1(int num, std::string str) {}
};
int main()
{
auto df = make_ctor_forwarder(10, "Ten");
//ctor_forwarder df(10, "Ten");
auto lazy_ctor = make_lazy_adaptor<Sample1>(10, "Ten");
return 0;
}