Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / C++ Template specialization Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
Randall Foster

March 09, 2005, 03:32 PM

I read somewhere that it is possible to define special cases of templates. I have been successful in doing so... but it seems like I have to redefine the whole template for the special case.

Does anyone know how I could specialize just one method in the template?
The name of this method in my code is custom(). As the name suggests it is a custom evaluation method that will allow me to extend the template. Currently it is defined as just an empty method in the template.

I know I could use a virtual function in the template then derive from the template class and define a new custom method. But I don't want to do this.

I wish to have the special case custom() method compile into the code in place of the defualt one. Thanks!

 
{FluffysWhole}

March 09, 2005, 04:45 PM

  1.  
  2.  
  3. template <typename Ty>
  4. class to_be_specialised
  5. {
  6. public:
  7.   void custom( void )
  8.   {  // stuff
  9.   }
  10. };
  11.  
  12. template <typename Ty>
  13. class not_specialised : to_be_specialised<Ty>
  14. {
  15. public:
  16.   void do_custom( void )
  17.   {  // call stuff
  18.     custom();
  19.   }
  20. };
  21.  
  22.  


Haven't tried it yet tho...

 
Randall Foster

March 09, 2005, 04:58 PM

No inheritence no virtuals.... just want a way to overwrite the method with different one in special cases. Thanks!

 
Fabian 'ryg' Giesen

March 09, 2005, 07:19 PM

I don't see how you would manage that avoiding both inheritance or virtuals (or solutions equivalent to virtuals, such as indirect calls).

 
{FluffysWhole}

March 10, 2005, 03:01 AM

Well, The solution I posted had no virtuals. But if you want no inheritance then:

  1.  
  2. template <typename Ty>
  3. class to_be_specialised
  4. {
  5. public:
  6.   void custom( void )
  7.   {  // stuff
  8.   }
  9. };
  10.  
  11. template <typename Ty>
  12. class not_specialised
  13. {
  14. public:
  15.   void custom( void )
  16.   {  // call stuff
  17.     mThisIsNotABaseClass.custom();
  18.   }
  19. private:
  20.   to_be_specialised<Ty> mThisIsNotABaseClass;
  21. };
  22.  


Though both solutions do what you origonally asked; inline, no virtuals and allowing the specialisation of just one method.

 
kiddygames

March 10, 2005, 05:52 AM

Hi,

I think what you want to do is that :

  1.  
  2. template<typename type>
  3. class A
  4. {
  5. public:
  6.   void custom( void )
  7.   {
  8.   // your standard code here
  9.     printf("in standard custom n");
  10.   }
  11. };
  12.  
  13. // and here comes the specialized method
  14. void A<float>::custom()
  15. {
  16.   // specialized code here
  17.   printf("in float customn");
  18. }
  19.  
  20.  
  21. int main(int argc, char* argv[])
  22. {
  23.   A<int>    test1;
  24.   A<float>  test2;
  25.  
  26.   test1.custom();
  27.   test2.custom();
  28.  
  29.   return 0;
  30. }
  31.  


but I think most compilers (visual at least) just don't know how to make partial specialization (you have to give the type of all your template argument to specialize your method)

Stéphane

 
Chris

March 10, 2005, 09:20 AM

Visual Studio .NET 2005 knows, and I think the .NET 2003 Toolkit also knows. .NET 2001 definitely does not.

 
Reedbeta

March 10, 2005, 01:13 PM

I don't think you're allowed to specialize one method of a class like this. You would have to specialize the whole class (which means having a specialized class definition and specialization for all the methods in the class).

At least, this doesn't work in VS.NET 2003.

 
Randall Foster

March 10, 2005, 05:17 PM

Thanks guys,


Ya I'm using VC 6 ... and you have to redefine the whole god damn class. You can't just specialize one method. I'll try .NET 2003 Toolkit if I can get it. It's about time I gave up trying to get away with VC6 forever. Although it has been so good to me.

Thanks!

 
kiddygames

March 11, 2005, 02:11 AM

I can't follow you guys ??

my first exemple works fine with VC 6, and .NET 2003 (I suppose it works also on gcc, but I didn't test it on gcc). When I talk about partial specialization, I talk about doing this :

  1.  
  2.  
  3. template<typename type1, typename type2>
  4. class A
  5. {
  6. public:
  7.   void custom( void )
  8.   {  // your standard code here    
  9.     printf("in standard custom n");
  10.   }
  11. };
  12.  
  13. // test partial specialization
  14. template<typename type2>
  15. void A<float,type2>::custom()
  16. {  // specialized code here  
  17.   printf("in float partially specialized customn");
  18. }
  19.  
  20.  


The compiler needs to know all template args to specialize the method. So this code will not work.

But my previous post was compiled and works fine. Here is another one working fine (on VC 6 and .NET 2003) where I just specialize one method of the class :

  1.  
  2.  
  3. template<typename type>
  4. class A
  5. {
  6. public:
  7.   void custom( void )
  8.   {  // your standard code here    
  9.     printf("in standard custom n");
  10.   }
  11.  
  12.   void custom1( void )
  13.   {  // your standard code here    
  14.     printf("in standard custom1 n");
  15.   }
  16. };
  17.  
  18. // and here comes the specialized method
  19.  
  20. void A<float>::custom()
  21. {  // specialized code here  
  22.   printf("in float customn");
  23. }
  24.  
  25. int main(int argc, char* argv[])
  26. {  
  27.   A<int>    test1;
  28.   A<float>  test2;
  29.   test1.custom();
  30.   test1.custom1();
  31.   test2.custom();
  32.   test2.custom1();
  33.   return 0;
  34. }
  35.  
  36.  


the output is :

in standard custom
in standard custom1
in float custom
in standard custom1

Stéphane

 
kiddygames

March 11, 2005, 02:17 AM



Reedbeta wrote: I don't think you're allowed to specialize one method of a class like this. You would have to specialize the whole class (which means having a specialized class definition and specialization for all the methods in the class). At least, this doesn't work in VS.NET 2003.


I've just tried my code with VC.NET 2003 and it works fine (the second test posted in the main thread also). You can specialize just one method with no problem with VC 6, .NET and gcc.

Stéphane

 
MGrunwald

March 11, 2005, 06:35 AM

To answer your question, you can not partially specialize member functions of class templates.

 
Randall Foster

March 12, 2005, 05:53 PM

Actually kiddygames is right.

It works for all MS compilers since vc 6.

Just make sure the actual specialized code is in a cpp file. Otherwise it won't link properly.

Thanks to all who posted!

 
Altair

March 13, 2005, 08:12 AM

It's called explicit specialization of a member function and syntax is like this:

  1.  
  2. template<>
  3. void A<int>::foo()
  4. {
  5. }
  6.  

Cheers, Altair

 
This thread contains 14 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.