consider the two template functions below:
template <typename T, typename A>
T* create(A i) {
return new T(i);
}
template <typename T, typename A>
T* create(A i) {
return new T();
}
They have the same signature but can be instantiated only once for given T and A.
Is there a way to apply some SFINAE idiom to the above template functions so that the version of the template function that can be instantiated is the first version if and only if T has a constructor accepting A as argument type, otherwise the second version with the default constructor will be instantiated, for example:
// S is given, cannot be changed:
struct S {
S(int) {}
}
int main() {
// the first template function should be instantiated since S has S(int)
create<S, int>(0); // the only call in the program
// ...
}
Use
is_constructible: