Tough Template Question

This is a discussion on Tough Template Question within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <complex> template <class T> class A { public: template <class R> void a(R); }; template <class ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    Tough Template Question

    Code:
    #include <iostream>
    #include <complex>
    
    template <class T>
    class A
    {
    public:
      template <class R>
      void a(R);
    };
    
    template <class T>
    template <class R>
    void A<T>::a(R param)
    {
      std::cout << param << " Default" << std::endl;
    }
    
    template <>
    template <class R>
    void A<int>::a(R param)
    {
      std::cout << param << " Specialized" << std::endl;
    }
    
    int main(int argc, char *argv[])
    {
      A<double> a;
      A<int> b;
      a.a("a: This call is the");
      b.a("b: This call is the");
      return 0;
    }
    The output of this code is...

    Code:
      a: This call is the Default
      b: This call is the Specialized
    Okay, so this code works, and the function a(R param) is specialized for the A<int> so that it prints the item and then " Specialized". What I want is the a(R param) function specialized for another templated type. For example if I wanted...

    Code:
    ...
    void A<std::complex<type> >::a(R param)
    {
      ...
    }
    I can't seem to get it to work. I've tried

    Code:
    template <class T>
    template <class R>
    void A<std::complex<T> >::a(R param)
    {
      ...
    }
    as well as

    Code:
    template <>
    template <class R, class T>
    void A<std::complex<T> >::a(R param)
    {
      ...
    }
    I can't seem to get this to work. If possible I need this to work for single functions, rather than specializing the entire class. The program I'm working on needs to use complex values, but it has about one hundered functions and only about 8 need to be specialized. Any help is greatly appreciated!
    Last edited by xxicemanx; 11-05-2010 at 02:27 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    In order to avoid specializing the entire class, you can implement the methods that you want to be specialized in a class of their own.

    Code:
    #include <iostream>
    #include <complex>
    
    template <class T>
    struct a_impl
    {
        template <class R>
        static void a(R);
    };
    
    template <class T>
    struct a_impl<std::complex<T> >
    {
        template <class R>
        static void a(R);
    };
    
    template <class T>
    class A
    {
    public:
      template <class R>
      void a(R param) { a_impl<T>::a(param); }
    };
    
    template <class T>
    template <class R>
    void a_impl<T>::a(R param)
    {
      std::cout << param << " Default" << std::endl;
    }
    
    template <class T>
    template <class R>
    void a_impl<std::complex<T> >::a(R param)
    {
      std::cout << param << " Specialized" << std::endl;
    }
    
    int main(int argc, char *argv[])
    {
      A<double> a;
      A<std::complex<int> > b;
      a.a("a: This call is the");
      b.a("b: This call is the");
      return 0;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template overloading question
    By plutino in forum C++ Programming
    Replies: 14
    Last Post: 02-27-2009, 01:10 AM
  2. One template question
    By noobcpp in forum C++ Programming
    Replies: 8
    Last Post: 12-05-2008, 11:02 AM
  3. simple template friend question
    By noodle24 in forum C++ Programming
    Replies: 2
    Last Post: 06-02-2006, 01:37 PM
  4. simple template question
    By Cobras2 in forum C++ Programming
    Replies: 5
    Last Post: 09-14-2005, 03:16 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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