Thread: Templates - Function Partial Specialization?

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    1,619

    Templates - Function Partial Specialization?

    Is it possible to specialize one function of a template (which accepts multiple parameters) based on one parameter only?

    For example:

    I declare a template class:


    Code:
    template<typename K, typename D>
    class RBTree{
    .
    .
    .
    };
    which has one function:

    Code:
    template <typename K, typename D>
    bool RBTree<K,D>::LessThan(const K t1, const K t2){
      return t1 < t2;
    }
    and I try to specialize it for the first parameter:

    Code:
    template <typename D>
    bool RBTree<char *, D>::LessThan(const char* t1,const char* t2){
       return strcmp(t1,t2) < 0;
    }
    but this fails with a compile error. Is what I want even possible? The only C++ text I have in front of me is pathetic at templates, and I can't find anything online that's doing this -- it's easy enough to specialize on ALL parameters; I only want to specialize on one parameter, and I only want to specialize two out of twenty-odd functions.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    Partial template specialization does not apply to functions,be they member or not member.
    As you mentioned,you can totally specialize member functions,but not partially specialize member functions.

  3. #3
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Question

    Code:
    template<typename K, typename D>
    class RBTree{
      public:
        bool LessThan(const K t1, const K t2);
        bool LessThan(const char* t1,const char* t2);  
    };
    
    template <typename K, typename D>
    bool RBTree<K,D>::LessThan(const K t1, const K t2){
      return t1 < t2;
    }
    
    template <typename K,typename D>
    bool RBTree<K,D>::LessThan(const char* t1,const char* t2){
       return strcmp(t1,t2) < 0;
    }
    You are not obligate to use all the template "parameters"(look to code above). Does this solve your problem??? Hope yes...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM