template specialization compile error on MSVC.NET 2003


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: template specialization compile error on MSVC.NET 2003

  1. #1
    Join Date
    May 2005
    Posts
    53

    template specialization compile error on MSVC.NET 2003

    This code below can compile properly on MSVC 6.0 but cannot on MSVC .NET 2003. Does anyone know what's going on ??

    Code:
    template <class T, class U>
    void ReadRNet(int& a, U** b, T* c = NULL)
    {
    	// DO something here.
    	return;
    }
    
    template<>
    void ReadRNet<int, char*>(int& a, char* b)
    {
    	// Do something here.
    	return;
    }
    
    // error C2912: explicit specialization; 'void ReadRNet<int,char*>(int&,char**  )' is not a specialization of a function template

  2. #2
    Join Date
    May 2005
    Posts
    53
    correct error message above.

    Code:
    template <class T, class U>
    void ReadRNet(int& a, U** b, T* c = NULL)
    {
    	// DO something here.
    	return;
    }
    
    template<>
    void ReadRNet<int, char*>(int& a, char* b)
    {
    	// Do something here.
    	return;
    }
    
    // error C2912: explicit specialization; 'void ReadRNet<int,char*>(int&,char*  )' is not a specialization of a function template

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    6 is less than standard and forgives a lot of "not quite correct" code. When in doubt, .net is normally correct.

    I think you need the template args in the <> on the bottom one?

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    You can't define a partial specialization of a function template in C++. This is possible only with class templates. You can however define an *overloaded* version of the said function, which means that you should get rid of the template <>
    part, and that would turn the "specialization" into an ordinary overloaded functions. In general if Visual 7.0 rejects code that compiled fine under VC++ 6, it's right.
    Danny Kalev

  5. #5
    Join Date
    May 2005
    Posts
    53
    Danny, what's partial & fully specialization ?

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    a partial specialization selects a subset of the template instances from the primary template. So if you have a template X<T>, in which T can be any type, a partial specialization thereof can look like X<T*> which applies only to pointers, but this is still an infinitr set of template specializations (i.e., instances) because you can define an endless number of pointer types. A full specialization is simply a template instance: X<int>, X<char*> etc.

    You can read more about this here:
    http://www.informit.com/guides/conte...plus&seqNum=49
    and
    http://www.informit.com/guides/conte...plus&seqNum=50
    Last edited by Danny; 11-17-2005 at 07:54 AM.
    Danny Kalev

Similar Threads

  1. What's Template Metaprogramming?
    By topcoder in forum C++
    Replies: 2
    Last Post: 07-26-2005, 07:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center