Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Intermediate
May 5, 2000

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 < class T1=char, class T2=char > // default types
class 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
Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date