Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > templates - par...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 3 Topic 9574 of 9593
Post > Topic >>

templates - partial specialization

by Bharath <tiromarch08@[EMAIL PROTECTED] > May 7, 2008 at 10:58 AM

Hello All,
I was trying to execute couple of examples to understand the partial
template specialization concept.
Here is one among those.

#include<iostream>
using namespace std;
template<class T> class my_vec
{
                public:
     my_vec(){std::cout<<"im class my_vec<class T> \n";}
};
template <class T>
class my_vec<T*>
{
         public:
   my_vec()
   {
           std::cout<<"Im in my_vec<T*> \n";
   }
};

template <class T> T mini(int i, int j);
main()
{
      my_vec<float*> p;
       system("pause");
}

In the C++ Programming language (3rd edition) by Bjarne Stroustrup, in
section 13.5, author had shown an example like this:

"To define a specialization that is used for every V e c t o r of
pointers and only for V e c t o r s of pointers,
we need a partial specialization:"

t e m p l a t e <c l a s s T > c l a s s V e c t o r <T *> : p r i v a
t e V e c t o r <v o i d *> {
p u b l i c :
t y p e d e f V e c t o r <v o i d *>B a s e ;
V e c t o r () :B a s e () {}
e x p l i c i t V e c t o r (i n t i ) :B a s e (i ) {}
T *& e l e m (i n t i ) { r e t u r n s t a t i c _ c a s t <T *&>(B a
s e :: e l e m (i )); }
T *& o p e r a t o r [](i n t i ) { r e t u r n s t a t i c _ c a s t
<T *&>(B a s e :: o p e r a t o r [](i )); }
/ / ...
};

basically he is extending the Vector<void*> to Vector<T*>. Why is it
so? In my example given above, I didn't derive from anything else. But
I think I'm solving the purpose to define a specialization that is
used for every my_vec of pointers and ONLY for my_vec of pointers.
I couldn't understand the author's intention correctly. Can someone
please explain ?

Thanks,
Bharath

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]




 3 Posts in Topic:
templates - partial specialization
Bharath <tiromarch08@[  2008-05-07 10:58:39 
Re: templates - partial specialization
Marcin Swiderski <sfid  2008-05-07 18:31:54 
Re: templates - partial specialization
Carl Barron <cbarron41  2008-05-08 03:28:24 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu May 15 0:45:03 CDT 2008.