Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: C++
Expertise: Intermediate
Mar 14, 2005



Application Security Testing: An Integral Part of DevOps

Restrict the Types of Template Argument for a Template Class

You can restrict the type of template arguments for a template class by using the Forward template declaration.

Suppose you have a template class and you want to instantiate it only for types int and double:

template<class type>
class test
     test(type x);
     type getValue();
     type m_value;

#include "test.h"

template test<int>;//Forward template declaration
template test<double>;//Forward template declaration

template<class type >
test<type>::test(type t):m_value(t)

template<class type>
type test< type >:: getValue ()
     return m_value;

#include "test.h"

int main()
     test<int> t1(123);		//This will compile
     test<double> t2(6.5);	//This will compile
     cout << t1. getValue () << endl;
     cout << t2. getValue () << endl;
    // test<const char*> t3(sau);	//This will not compile
     return 0;
Saurabh Ramya
Comment and Contribute






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



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