Lets say I have two classes
template <int A_, int B_>
class One {
public:
static constexpr auto A = A_;
static constexpr auto B = B_;
const int C;
One(int C) : C(C) {}
};
class Two {
public:
const int A;
const int B;
const int C;
Two(int A, int B, int C) : A(A), B(B), C(C) {}
};
The only difference being that class One
takes the A
and B
parameters at compile time, while Two
takes the parameters at runtime. There exist multiple operations on these essentially equivalent types, so they can be written generically:
template <typename T>
auto min(const T& a, const T& b) {
if (a.C < b.C) {
return T(a.A+b.A, a.B+b.B, a.C);
} else {
return T(a.A+b.A, a.B+b.B, b.C);
}
}
The issue with the above is the construction of the output object. The two types One
and Two
can be used in the same way to access the A
, B
, and C
members, getting the types of them, etc. However, because there is a disparity in the construction of the objects (feeding function arguments vs feeding template arguments), if the operation requires the creation of a new object, it cannot be written generically.
I have tried non-type template argument deduction, but that is not currently possible. Is there any way around this? Or am I doomed to copying code?