Thread: A Question On Specializaton. Please Help!

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216

    A Question On Specializaton. Please Help!

    Suppose I write a class as follows:

    Code:
    template <class T1>   
    class have_a_try
    {  
    public: 
        template <class T2> 
        T2 test( const T2&, const T2& );  
    private:  
        T1 se;  
    };  
    
    template<class T1>
    template<class T2>  
    T2 have_a_try<T1>::test( const T2& a, const T2& b )  
    { 
        return a; 
    }
    As you could see, test is a template member. Now, I'd like to make a special version of test to handle arguments of type int (test<int>) no matter how the template class have_a_try is instantiated, that's, no matter have_a_try is instantiated for type int, double, string... if the actual arguments passed to that instance is of type int, the specialized version of test is called. I've tried the following code but failed.

    Code:
    template<class T1>
    template<>  
    int have_a_try<T1>::test<int>( const int& a, const int& b )  
    { 
        return a; 
    }
    One of my friend asked me this question just now but I still cannot solve it. I guess it's impossible to make a specialized version of a member template while leaving the class template generic.

    Dear all, please help me.
    Last edited by Antigloss; 10-19-2006 at 08:19 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM