Advertisement

Template question...

Started by March 08, 2001 05:46 PM
2 comments, last by farmersckn 23 years, 11 months ago
Say I create a template: template class Dilly { ... } Then I create a specialization of this template: template class Dilly { ... } Now, here''s the question, when I create a typedef of an int, will the template generated be of int type, or of type TypedefName? typedef int Dally; Dilly dd; Will dd be of type Dilly or Dilly? Thanks. farmersckn
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.
Ummm... before I can answer, let me fix some things. I don''t see any difference in your two template definitions. The other problem is when you declare your templated classes. It should be:

    typedef int Dally;  Dilly <Dally> dd;  


So regarding your first question, Dilly won''t be of type anything cuz you don''t give it any type. Now your second question is confusing and I''m not sure if you phrased it correctly, so can''t answer. I think you "specialize" a template by doing this:

    ...  typedef Dilly <Dally> DillyDally;  


So now you can use "DillyDally" in place of "Dilly ".

This is how STL works. Everything is a template and most classes are typedef-ed versions of those templates.

Jinushaun
Advertisement
This is one of the things that's pretty easy to test yourself, I bet you'd get the answer at least 100 times faster if you just wrote a test program for it. Now that I tell you, can you really trust me ? Muahhahaha...errm.
C's typedefs aren't that powerful (unlike Ada's for instance), basically since C deploys a weak type-system. So the type-system will derive Dally to int and a declaration like:
  template<> class A<Dally>;// andtemplate<> class A<int>;  

will mean the same thing. Here's a small example program to play around with:
        typedef int MyInt;template<class T> class A{public:  A() {printf("General!\n");}};template<> class A<MyInt>{public:  A() {printf("Special!\n");}};int main(){  A<double> a1;  A<int> a2;  A<MyInt> a3;  return 0;}  

Testing yourself is (and will always be) the best way to learn!

Edited by - amag on March 9, 2001 4:16:28 AM
Thanks, that''s what I needed to know. I normally would have just tested this out, but I had to completely wipe my hard drive just recently, and I haven''t got a compiler right now. Gonna take a trip to Borland.com...
farmersckn
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.

This topic is closed to new replies.

Advertisement