specialized member function takes precedence over generic template member function

Discussion in 'C++' started by bluekite2000@gmail.com, Jul 20, 2005.

  1. Guest

    Here is the compilable code, along w/ the error

    #include<iostream>
    #include<complex>
    typedef std::complex<float> ComplexSingle;
    using namespace std;
    template<typename T> class X
    {
    private:
    T number;
    public:
    X(T value)
    {
    number=value;
    }
    template<typename Other> X(Other Y)
    {
    assign(Y);
    }
    template<typename Other> void assign(Other Y);


    T return_number()
    {
    return number;
    }
    };

    template<typename T>
    template<typename Other> void X<T>::assign( Other Y)
    {
    number=Y.return_number();
    }

    template<> template<typename ComplexSingle> void
    X<float>::assign(ComplexSingle Y)
    {
    number=norm(Y.return_number());
    }
    int main (void)
    {
    //this works fine
    ComplexSingle a(2,3);
    X<ComplexSingle> A(a);
    X<float> B(A);


    //error
    //In member function `void X<T>::assign(Other) [with Other =
    //X<double>, T = float]':
    //ex1.cc:16: instantiated from `X<T>::X(Other) [with Other =
    // X<double>, T = float]'
    //ex1.cc:45: instantiated from here
    //ex1.cc:35: error: no matching function for call to `norm(double)'

    X<double> C(3.3);
    X<float> D(C);
    return 0;
    }
     
    , Jul 20, 2005
    #1
    1. Advertising

  2. Re: specialized member function takes precedence over generic templatemember function

    wrote:
    > Here is the compilable code, along w/ the error
    >
    > #include<complex>


    > template<typename T> class X
    > {
    > T number;
    > public:
    > [..]
    > T return_number()
    > {
    > return number;
    > }
    > };
    >
    > template<> template<typename ComplexSingle> void
    > X<float>::assign(ComplexSingle Y)
    > {
    > number=norm(Y.return_number());


    'norm' is a template declared for all complex<T>. When you call it like
    that with 'Y' as 'X<double>', it looks around and cannot find any 'norm'
    for 'double', then it sees that there is 'norm<T>(const complex<T>&)',
    but since the argument you give is 'double', it can't understand what 'T'
    should be. This is not one of contexts from which 'T' can be deduced.

    What are you trying to achieve, anyway? I can only recommend defining
    your own 'norm':

    double norm(double a) {
    return fabs(a); // or whatever
    }

    > }


    V
     
    Victor Bazarov, Jul 20, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Allan Schwartz
    Replies:
    1
    Views:
    399
    Victor Bazarov
    Aug 9, 2004
  2. BigMan
    Replies:
    3
    Views:
    1,436
    BigMan
    Apr 22, 2005
  3. Ingo
    Replies:
    2
    Views:
    567
  4. Igor R.
    Replies:
    2
    Views:
    223
    Igor R.
    Jun 1, 2011
  5. Replies:
    1
    Views:
    289
Loading...

Share This Page