I’d like to be able to construct an A or B without having to think about the number of constructor arguments.
The second constructor is not legal C++ but I wrote it like this as an attempt to express what I want.
Is there an enable_if trick to selectively enable one of the constructors?
(e.g. depending on the number of constructor arguments of A and B.)
I need this to test about 15 classes with 1, 2 or 3 constructor arguments.
struct A
{
A(int x)
{
}
};
struct B
{
B(int x, int y)
{
}
};
template<typename T>
struct Adaptor // second constructor is illegal C++.
{
T t;
Adaptor(int x, int y)
: t(x)
{
}
Adaptor(int x, int y) // error: cannot be overloaded
: t(x, y)
{
}
};
int main()
{
Adaptor<A> a(1,2);
Adaptor<B> b(1,2);
return 0;
}
A variant of @Aaron’s approach: