Tough Template Questions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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...

1
2
  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...

1
2
3
4
5
...
void A<std::complex<type> >::a(R param)
{
  ...
}


I can't seem to get it to work. I've tried

1
2
3
4
5
6
template <class T>
template <class R>
void A<std::complex<T> >::a(R param)
{
  ...
}


as well as

1
2
3
4
5
6
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!
Are you getting compile errors? Or what is the output?

A quick (maybe not the best) workaround:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <complex>

template <class T>
class A
{
public:

    template <class R>
    void a(R);

    template <class C, class D>
    friend void a_helper(A<std::complex<C> > & alpha, D param);
};

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;}

template <>
template <class R>
void A<std::complex<int> >::a(R param) {a_helper(*this,param);}

template <>
template <class R>
void A<std::complex<double> >::a(R param) {a_helper(*this,param);}

template <class T, class R> void a_helper(A<std::complex<T> > & alpha, R param)
{std::cout << param << "Complex Specialized" << std::endl;}

int main(int argc, char *argv[])
{
    A<double> a;
    A<int> b;
    A<std::complex<int> > c;
    A<std::complex<double> > d;

    a.a("a: This call is the ");
    b.a("b: This call is the ");
    c.a("c: This call is the ");
    d.a("d: This call is the ");

    return 0;
}
Last edited on
Jsmith sorry it doesnt compile I'll post the error when I get home

Roshi

Thanks for the reply, this is an interesting workaround that I will need to use if I can't find something better but I would really like to be able to make the template more general so that I can add new classes later without going back to modify the original header.
Topic archived. No new replies allowed.