CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    2,338

    What is template <>?

    Hello!

    I got some 3rd-party headers. I can compile them with VC++.net 2003, but not with VC++ 6.0. But my project is written in 6.0 and I have no time to convert in into a VC++ 7-project (I discovered a lot of errors when trying to do so).

    The problem appear in lines like:
    Code:
    // This works fine
    template <typename T>
    inline bool MyTyp::func() const
    { ...
    }
    
    // This doesn't
    template <>
    bool MyTyp::func<unsigned short>() const; // error C2143: syntax error : missing ';' before '<'
    I was wondering what the 2nd function-definition means and how I can get it to compile under VC++ 6.0?

  2. #2
    Join Date
    Aug 1999
    Posts
    586

    Re: What is template <>?

    This is referred to as a template specialization which you can read about in your favorite C++ book. Unfortunately, VC 6.0's support for templates is weak and sometimes buggy. Moreover, member function templates aren't supported at all when defined outside of the class. Note that VC6 was also written before the official C++ standard was ratifiied in 1998 (both occurred around the same time). You're out of luck IOW (and better off taking the plunge into a later compiler if possible - note however that other potential issues may also surface - non-compliant and potentially buggy C++ code that you could get away with in VC6 isn't so easy in later versions of the compiler - this is for your own good however)

  3. #3
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    2,338

    Re: What is template <>?

    Hi Sef, thanks for your reply. I assume that after 6 ServicePacks there was no update to make to compiler C++-standard-conform?

    I would like to change to C++ 7.0 (.net 2003), but the IDE is much less comfortable then it is in 6.0. And (you know for sure): Never change your running system

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: What is template <>?

    Quote Originally Posted by martho
    I assume that after 6 ServicePacks there was no update to make to compiler C++-standard-conform?
    AFAIK, MS never claimed it ever would conform. (Otherwise why anybody would buy next version? )

    I would like to change to C++ 7.0 (.net 2003), but the IDE is much less comfortable then it is in 6.0.
    Highly arguable sentence. Let's better say, this is the matter of time and habit.

    And (you know for sure): Never change your running system
    ...until it fails to fit your reqirements.

    PS. Wisemen say: never say never again...
    Best regards,
    Igor

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: What is template <>?

    Quote Originally Posted by martho
    I would like to change to C++ 7.0 (.net 2003), but the IDE is much less comfortable then it is in 6.0.
    So I thought at first, but now I believe the opposite. However, the 7.1 IDE has some pretty buggy (I believe) parts that sometimes draw me mad.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    May 2005
    Posts
    121

    Re: What is template <>?

    Quote Originally Posted by Sef
    Note that VC6 was also written before the official C++ standard was ratifiied in 1998 (both occurred around the same time).
    True, but certainly not the reason for non-compliance. As Herb Sutter admitted, Microsoft stopped turning up to the C++ standards committee meetings because they didn't care about complying with the standard.

    Markus

  7. #7
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: What is template <>?

    Quote Originally Posted by martho
    I was wondering what the 2nd function-definition means ...
    Template specialization

    A template specialization allows a template to make specific implementations for when the pattern is of a determined type. For example, suppose that the class template 'pair' included a function to return the result of the module operation between the objects contained in it, but it should only work when the contained type is 'int' otherwise it should return 0:
    Code:
    // Template specialization
    #include <iostream>
    
    template<typename T>
    class pair
    {
      T value1;
      T value2;
    
    public:
      pair(T first, T second)
      {
        value1 = first;
        value2 = second;
      }
    
      T module() { return 0; }
    };
    
    template<>
    class pair<int>
    {
      int value1;
      int value2;
    
    public:
      pair(int first, int second)
      {
        value1 = first;
        value2 = second;
      }
    
      int module();
    };
    
    template<>
    int pair<int>::module()
    {
      return value1 % value2;
    }
    
    int main()
    {
      pair<int>MyInt(100, 75);
      pair<float>MyFloat(100.0, 75.0);
    
      std::cout << MyInt.module() << std::endl;
      std::cout << MyFloat.module() << std::endl;
    
      return 0;
    }
    The specialization is defined this way:
    Code:
    template<>
    class class_name<type>
    The specialization is part of a template, for that reason the declaration must begin with 'template<>'. And indeed because it is a specialization for a concrete type the generic type cannot be used in it, as well as the first angle-brackets '<>' must appear empty. After the class name the type must be included that is being specialized enclosed between angle-brackets '<>'.

    When you specialize a type of a template you must also define all the members adequating them to the specialization (in the example above you have had to include its own constructor, although it is identic to the one in the generic template). The reason is that no member is "inherited" from the generic template to the specialized one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured