Just for the fun, was trying to propose something like:
struct Object {
// SFINAE
template<class T,
typename = typename std::enable_if<std::is_same<T, int>::value>::type>
Object(T a = 0, T b = 0){
std::cout << "ctor with two ints and defaults" << std::endl;
}
Object(){
std::cout << "empty ctor" << std::endl;
}
};
int main() {
Object o1;
Object o2(8, 9);
Object o3<int>();
}
But unfortunately the creation of o3
in that way is not allowed, as apparently you cannot explicitly specify template argument for a constructor, see: Can the template parameters of a constructor be explicitly specified? (answer is: no...)
Anyway, this was just for fun.
I found the answer by Scab in the comment above the most appropriate for the imagined need - use named ctor idiom - http://isocpp.org/wiki/faq/ctors#named-ctor-idiom:
struct Object {
static Object emptyCtor() { return Object{}; }
static Object defaultIntegers() { return Object{0, 0}; }
static Object create(int a, int b) { return Object{a, b}; }
protected:
Object(int a, int b){
std::cout << "ctor with two ints" << std::endl;
}
Object(){
std::cout << "empty ctor" << std::endl;
}
};
int main() {
Object o1 = Object::emptyCtor();
Object o2 = Object::defaultIntegers();
Object o3 = Object::create(8, 9);
}