[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppTemplateClass
See compile time assert for details.
Comparing this with polymorphism, one notes that when using ?CppTemplate, templates, types cannot be cast to each other. Using polymorphism this is legal using a dynamic_cast.
In other words, when using templates, a type is defined at compile time, whereas using polymorphism a type's behaviour can be changed at runtime.
Also note, that in the example above, the Person<angry> does not have a SayHello method and therefore can not be called. When using polymorphism either this (virtual) method would have to defined, or some other dirty casts would have been needed.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppTemplateClass
(C++) Template class
A template class is a "class parameterized by types, values, or templates. The template arguments necessary to identify the class to be generated for the class template must be provided where a template class is used" [1].
template <class T>
struct MyClass
{
T mValue;
};
int main()
{
MyClass<int> m;
m.mX = 10;
}
compile time assert
A template class can be used for a compile time assertion:
template <bool> struct CompileTimeAssert;
template<> struct CompileTimeAssert<true> {};
int main()
{
CompileTimeAssert<((1+1)==2)> x;
//The line below will give the following compile-time errors:
// [C++ Error] : Undefined structure 'CompileTimeAssert<0>'
// [C++ Error] : Size of 'y' is unknown or zero
CompileTimeAssert<((1+2)==2)> y;
}
See compile time assert for details.
A template class is a type
In the examle below, a simple struct is defined, called Person with a template argument, a Mood, which is an enumeration. The example below shows that a Person<happy> cannot be converted to a Person<sad>, except by a dirty reinterpret_cast using a temporary pointer.enum Mood{ happy, sad, angry }; template<Mood> struct Person; template<> struct Person<happy> { void sayHello() const { std::cout << "Hello
- include <iostream>
" << std::endl; } }; template<> struct Person<sad> { void sayHello() const { std::cout << "Hello
" << std::endl; } }; template<> struct Person<angry> { //An angry person cannot say hello }; template <Mood T> void SayHello(const Person<T>& person) { person.sayHello(); } int main() { //Create a happy person Person<happy> kristel; //Let the happy person say hello SayHello(kristel); //Luckily cannot cast a Person<happy> to Person<sad> : // Cannot cast from 'Person<0>' to 'Person<1>' //SayHello(reinterpret_cast<Person<sad> >(kristel)); sayHello(*(reinterpret_cast<Person<sad>* >(&kristel))); }
Comparing this with polymorphism, one notes that when using ?CppTemplate, templates, types cannot be cast to each other. Using polymorphism this is legal using a dynamic_cast.
In other words, when using templates, a type is defined at compile time, whereas using polymorphism a type's behaviour can be changed at runtime.
Also note, that in the example above, the Person<angry> does not have a SayHello method and therefore can not be called. When using polymorphism either this (virtual) method would have to defined, or some other dirty casts would have been needed.
Reference
- 1) Bjarne Stroustrup's C++ glossary. http://www.research.att.com/~bs/glossary.html#Gclass
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
