A Question On Specializaton. Please Help!

This is a discussion on A Question On Specializaton. Please Help! within the C++ Programming forums, part of the General Programming Boards category; Suppose I write a class as follows: Code: template <class T1> class have_a_try { public: template <class T2> T2 test( ...

  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212

    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.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I guess it's impossible to make a specialized version of a member template while leaving the class template generic.
    Correct. Presently you can't specialize a template member function without specializing the enclosing class. Consider overloading as an alternative.
    My best code is written with the delete key.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Something like this works for me but I'm not sure if it is guaranteed to work.

    Code:
    #include <iostream>
    #include <string>
    
    class A
    {
        public:
            template <class T>
            T test(T a) 
            {
                std::cout << "template version" << std::endl;
                return a;
            }
            int test(int a)  //overloaded int version
            {
                std::cout << "int version" << std::endl;
                return a;
            }
    };
    
    int main()
    {
        A a;
        std::cout << a.test(4) << std::endl; //prints int version here
        std::cout << a.test<int>(42) << std::endl; //prints template however
        std::cout << a.test<std::string>("Hello") << std::endl;
        std::cin.get();
    }
    Edit: yes, this is guaranteed to work. And the type specifications could be omitted as the compiler should be able to deduce it on its own. In this case non-template functions always are considered first.
    Last edited by anon; 10-19-2006 at 09:36 AM.

  4. #4
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Quote Originally Posted by Prelude
    >I guess it's impossible to make a specialized version of a member template while leaving the class template generic.
    Correct. Presently you can't specialize a template member function without specializing the enclosing class. Consider overloading as an alternative.
    Yes. That's what I am thinking about.

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, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21