devxlogo

Default Arguments in a Template Instantiation

Default Arguments in a Template Instantiation

Templates can have default type parameters. The compiler uses these default types when you don’t provide them explicitly in a template instantiation. For example:

 template  // default typesclass E { public:  E( T1 t1, T2 t2 ) {m1=t1; m2=t2; } private:  T1 m1;  T2 m2;};

You instantiate a template with default type parameters like this:

 int main(){ E  e1(0,0);//OK, explicit types provided E  e2('a','b');//OK, using default types}

Make sure that your template instance has a pair of empty angular brackets after the template’s name when you create a template instance that uses default type values.Omitting the empty pair of angular brackets, as in the following declaration, is an error:

 E e3('a','c');   //compilation error;  missing
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist