Q:

Constructor that take parameters as default

My code is
template <class T1, class T2>
class MyClass
{

    T1 first;
    T2 second;
    public:
    //default constructor
    MyClass():first(T1()), second(T2()) {}
}

I want to have a single constructor which can take 0, 1 or 2 arguments (parameters as default). Here T1 and T2 can be both primitive as well as non-primitive types.

A:

Replace your existing constructor with this constructor:
MyClass(const T1& t1 = T1(), const T2& t2 = T2()):first(t1), second(t2) {}

