How do you specialize a template function in a template?

This is a discussion on How do you specialize a template function in a template? within the C++ Programming forums, part of the General Programming Boards category; Say you have: Code: template <typename T> class A { template <typename K> void foo(T, K); }; Let's say I ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    How do you specialize a template function in a template?

    Say you have:

    Code:
    template <typename T>
    class A {
       template <typename K>
       void foo(T, K);
    };
    Let's say I want to specialize "foo" for parameters <T = int, K = double>. What's the syntax for doing this?

    Code:
    template<> template<>
    void A<int>::foo<double>(int i, double d) {;}
    Would this work? It's just a problem that came to me while I was walking around, so I'm typing this from a public computer. I don't have a compiler to try it out.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by cunnus88 View Post
    Let's say I want to specialize "foo" for parameters <T = int, K = double>. What's the syntax for doing this?

    Code:
    template<> template<>
    void A<int>::foo<double>(int i, double d) {;}
    Looks correct to me.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Wow. A hunch I had is actually right. But that syntax is pretty funky. I mean
    Code:
    template<> template<>
    Really?

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by cunnus88 View Post
    Wow. A hunch I had is actually right. But that syntax is pretty funky. I mean
    Code:
    template<> template<>
    Really?
    Yep. The multiple template specs nest outward-inward, so the first template<> is specializing on T, the second on K
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Quote Originally Posted by brewbuck View Post
    Yep. The multiple template specs nest outward-inward, so the first template<> is specializing on T, the second on K
    So if I want to partially specialize it just for K, then it would be
    Code:
    template<typename T> template<>
    typename void A<T>::foo(double)(){}
    Is that correct?

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by cunnus88 View Post
    So if I want to partially specialize it just for K, then it would be
    Code:
    template<typename T> template<>
    typename void A<T>::foo(double)(){}
    Is that correct?
    Should be.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM

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