Consider the following builder-like class, which ultimately allows me to construct an object with both certain (runtime) values for member variables, as well as embedding some behavior which is carried by several (compile-time) types.
The same build allows one to update member variables (the usual builder pattern), as well as change template type parameters associated with the type-carried state of the builder (only shown with a couple of template type parameters and members, but in practice, there would be more):
template <typename T1 = DefaultT1, typename T2 = DefaultT2>
class Builder {
int param1, param2;
Builder(int param1, int param2) : param1{param1}, param2{param2} {}
public:
Builder() : Builder(default1, default2) {}
// methods to change param1 and param2 not shown
/* return a new Builder with T1 changed to the given T1_NEW */
template <typename T1_NEW>
Builder<T1_NEW, T2 > withT1() { return {param1, param2}; }
template <typename T2_NEW>
Builder<T1 , T2_NEW> withT2() { return {param1, param2}; }
Foo make() {
// uses T1 and T2 to populate members of foo
return Foo{ typename T1::member, typename T2::another };
}
};
Note the withT1<>
and withT2<>
methods which allow you to return a new builder with a different type for T1
or T2
respectively. The bodies for these methods are identical: return {param1, param2};
, and in practice much more complicated than shown here (e.g., if there are many parameters).
I'd like to factor the body out into some method which does the construction, like:
template <typename T1_, typename T2_>
Builder<T1_, T2_> copy() { return {param1, param2}; }
and then each withT*
method could just call copy.
However, it isn't clear to me how to avoid including the fully qualified type of Builder
in the call:
template <typename T1_NEW>
Builder<T1_NEW, T2 > withT1() { return copy<T1_NEW, T2>(); }
Here the cure is worse than the original poison since I need to qualify each copy call with <T1_NEW, T2>
(and this is different for each withT*
method). Is there some way I can refer to the return type or another type of deduction which I can use to call copy()
in the same way in each function?
I'm writing in C++11, but discussion of how a C++11 solution could be improved in later standards is also welcome.
I dont have a solution for C++11, but as you said yourself, a C++14 may be helpful for others.
If I understood correctly, you need a class that stores arbitrary arguments with a convenient way to pass all arguments to a constructor. This can be achieved using variadic template arguments and std::tuple
:
#include <tuple>
template <typename... Args>
class Builder
{
public:
explicit Builder(Args... args)
: arg_tuple(std::forward<Args>(args)...)
{}
template <typename T>
T make()
{
return std::make_from_tuple<T>(arg_tuple);
}
template <typename T>
Builder<Args..., T> with(T t)
{
return std::make_from_tuple<Builder<Args..., T>>(std::tuple_cat(arg_tuple, std::make_tuple(std::move(t))));
}
private:
std::tuple<Args...> arg_tuple;
};
template <typename... Args>
Builder<Args...> make_builder(Args... args)
{
return Builder<Args...>(std::forward<Args>(args)...);
}
Usage:
struct Foo
{
Foo(int x, int y)
: x(x), y(y)
{}
int x;
int y;
};
struct Bar
{
Bar(int x, int y, float a)
: x(x), y(y), a(a)
{}
int x;
int y;
float a;
};
int main()
{
auto b = make_builder().with(5).with(6);
auto foo = b.make<Foo>(); // Returns Foo(5, 6).
auto b2 = b.with(10.f);
auto bar = b2.make<Bar>(); // Returns Bar(5, 6, 10.f).
}
While std::make_from_tuple
is C++17, it can be implemented using C++14 features:
namespace detail
{
template <typename T, typename Tuple, std::size_t... I>
constexpr T make_from_tuple_impl(Tuple&& t, std::index_sequence<I...>)
{
return T(std::get<I>(std::forward<Tuple>(t))...);
}
}
template <typename T, typename Tuple>
constexpr T make_from_tuple(Tuple&& t)
{
return detail::make_from_tuple_impl<T>(
std::forward<Tuple>(t),
std::make_index_sequence<std::tuple_size_v<std::remove_reference_t<Tuple>>>{});
}
You can introduce a builder proxy that has an implicit conversion to save yourself some typing:
template<typename T1, typename T2>
struct Builder;
struct BuilderProxy
{
int param1, param2;
template<typename T1, typename T2>
operator Builder<T1, T2>() const { return {param1, param2}; }
};
template <typename T1, typename T2>
struct Builder {
int param1, param2;
Builder(int param1, int param2) : param1{param1}, param2{param2} {}
BuilderProxy copy() { return {param1, param2}; }
template <typename T1_NEW>
Builder<T1_NEW, T2 > withT1() { return copy(); }
template <typename T2_NEW>
Builder<T1 , T2_NEW> withT2() { return copy(); }
};
int main() {
Builder<int, int> a(1, 2);
Builder<double, double> b = a.withT1<double>().withT2<double>();
}
Not sure I understood your problem 100%. Let me try a tentative answer anyway: I added to your code snippet a templated implicit conversion operator which is calling a reinterpret_cast
template<typename U1, typename U2>
operator Builder<U1, U2>(){ return *reinterpret_cast<Builder<U1, U2>*>(this); }
This is generally hacky and unsafe, but in your case it does the job. it allows the withT1 and withT2 member functions to be like
template <typename T1_NEW>
Builder<T1_NEW, T2 > withT1() { return *this; }
template <typename T2_NEW>
Builder<T1 , T2_NEW> withT2() { return *this; }
the snippet I used for testing the code is attached below
#include <type_traits>
template<typename T1, typename T2>
struct Foo;
template<>
struct Foo<int, double>{
int mInt;
double mDouble;
};
template<>
struct Foo<char, double>{
char mChar;
double mDouble;
};
struct t1{
using type = int;
static type member;
};
struct t2{
using type = double;
static type another;
};
struct tt1{
using type = char;
static type member;
};
template <typename T1 = t1, typename T2 = t2>
class Builder {
// int param1, param2;
Builder(int param1, int param2) : param1{param1}, param2{param2} {}
public:
int param1, param2;
Builder() : Builder(0, 0) {}
template<typename U1, typename U2>
operator Builder<U1, U2>(){ return *reinterpret_cast<Builder<U1, U2>*>(this); }
template <typename T1_NEW>
Builder<T1_NEW, T2 > withT1() { return *this; }
template <typename T2_NEW>
Builder<T1 , T2_NEW> withT2() { return *this; }
Foo<typename T1::type, typename T2::type> make() {
// uses T1 and T2 to populate members of foo
return Foo<typename T1::type, typename T2::type>{T1::member, T2::another};
}
};
int main(){
Builder<t1, t2> b;
auto c = b.withT1<tt1>();
static_assert(std::is_same<decltype(c), Builder<tt1, t2>>::value, "error");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With