Wednesday, April 11, 2007

Template - Specialization

For creating a specialized template:

// Generalized template : A must
template
class Generic
{
public:
Generic (T& value, C& constant)
{
std::cout << "Generic::Ctor " <<>
std::cout << "Value: " <<>
<< "Constant: " <<>
}
~Generic ()
{
std::cout << "Generic::Dtor" <<>
}
};

// Fully specialized template class
template <>
class Generic
{
public:
Generic (float& value, double& constant)
{
std::cout << "Generic::Ctor " <<>
std::cout << "Value: " <<>
<< "Constant: " <<>
}
~Generic ()
{
std::cout << "Generic::Dtor " <<>
}
};

// Partial specialization template class
template
class Generic
{
public:
Generic (T& value, int& constant)
{
std::cout << "Generic::Ctor "
<< "Value: " <<>
<< "Constant: " <<>
}
~Generic ()
{
std::cout << "Generic::Dtor " <<>
}
};

int
main ()
{
float f1 = 5.5f;
int i1 = 5;
double d1 = 3.3;

Generic temp1 (f1, d1); // fully specialized
Generic temp (f1, i1); // Partial specialization

return 0;
}

0 Comments:

Post a Comment

<< Home