unexpected compile error w/ member function specialization of a template class

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

  1. Guest

    I have
    Vector<complex<float> > V(5);
    V.rand();
    Vector<float> V1(V); //specialized function here to return norm(V).
    This works fine

    Vector<double> V2(5);
    V2.rand();
    Vector<float> V3(V2);//error no matching function for call to
    norm(double)

    Apparently Vector<float> V3(V2); calls the specialized function, which
    it shouldnt . If I comment out the specialized function, Vector<float>
    V3(V2); works fine (i.e it calls template<typename T>
    template<typename Other> Vector<T>::Vector(const VectorView<Other>&
    Vin) )
    Is this a hidden namespace problem? If so how do I fix it?
    Regards,
     
    , Jul 19, 2005
    #1
    1. Advertising

  2. Re: unexpected compile error w/ member function specialization ofa template class

    wrote:
    > I have
    > Vector<complex<float> > V(5);


    What's "Vector"?

    > V.rand();
    > Vector<float> V1(V); //specialized function here to return norm(V).
    > This works fine


    OK, I'll take your word for it.

    > Vector<double> V2(5);
    > V2.rand();
    > Vector<float> V3(V2);//error no matching function for call to
    > norm(double)
    >
    > Apparently Vector<float> V3(V2); calls the specialized function, which
    > it shouldnt .


    Really? "Apparently"? OK...

    > If I comment out the specialized function, Vector<float>
    > V3(V2); works fine (i.e it calls template<typename T>
    > template<typename Other> Vector<T>::Vector(const VectorView<Other>&
    > Vin) )
    > Is this a hidden namespace problem? If so how do I fix it?


    I have no idea. Read FAQ 5.8.

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

  3. Guest

    Yeah my code is kinda long so i chose not to post it. I just dont
    understand why it works for almost all conversions (from
    int,float,double,complex float, complex double to
    int,float,double,complex float, complex double) except for float to
    double, in which case it calls my specialized template function
    (Complex float to float) and complains that norm(double) has no
    matching function. It seems to me double gets promoted to complex float
    implicitly or something.
     
    , Jul 19, 2005
    #3
  4. Re: unexpected compile error w/ member function specialization ofa template class

    wrote:
    > Yeah my code is kinda long so i chose not to post it.


    My crystal ball is in the repair shop, so I chose not to guess.

    > I just dont
    > understand why it works for almost all conversions (from
    > int,float,double,complex float, complex double to
    > int,float,double,complex float, complex double) except for float to
    > double, in which case it calls my specialized template function
    > (Complex float to float) and complains that norm(double) has no
    > matching function. It seems to me double gets promoted to complex float
    > implicitly or something.
    >


    There is no implicit promotion from double to _anything_. The only
    standard floating point promotion is float -> double. There are floating
    point _conversions_, which allow an rvalue of one FP type to be converted
    to an rvalue of another FP type. std::complex<something> is essentially
    a user-defined type, so any conversion from 'something' to it would *not*
    be a standard conversion or a promotion, but a user-defined conversion.

    AFAICS, std::complex<T> is defined with a converting constructor with two
    arguments both of which have a default argument value, T(). RTFM. That
    constructor can be used to convert 'float' to 'std::complex<float>' or
    even 'double' to 'std::complex<float>'. IOW, you may write

    std::complex<float> cf(1.0);

    which defines 'cf' and initialises it with real == 1.f, imaginary == 0.f.

    How it reflects on the functionality of your special code, I've no idea.

    V
     
    Victor Bazarov, Jul 19, 2005
    #4
  5. Guest

    Ok here it is:
    template <typename T>
    class Vector
    {
    private:
    int _size;
    void VecAlloc(int size)
    {
    }
    public:
    template<typename Other> Vector(const Vector<Other>& Vin)
    {
    _size=Vin.size();
    VecAlloc(Vin.size());
    vassign(Vin);
    }
    .....
    template<typename Other> void vassign( const Vector<Other>& Vin);
    .....
    };
    template<typename T>
    template<typename Other> void Vector<T>::vassign(const Vector<Other>&
    Vin)
    {
    for (int i=0;i<Vin.size();i++)
    (*this)(i)=Vin(i);
    }

    template<> template<typename ComplexSingle> void
    Vector<float>::vassign(const Vector<ComplexSingle>& Vin)
    {
    for (int i=0;i<Vin.size();i++)
    (*this)(i)=norm(Vin(i));
    }

    .....
    int main (void)
    {

    Vector<double> V(5);
    Vector<float>V1(V); //compile errror
    //instantiated from `Vector<T>::Vector(const Vector<Other>&) [with
    Other = double, T = float]'
    //test2.cc:11: instantiated from here
    //matlib2.h:290: error: no matching function for call to `norm(double)'

    return 1;
    }
     
    , Jul 19, 2005
    #5
  6. Dan Cernat Guest

    The code you posted doesn't compile. Even I take out the line you say, it
    still gives a bunch of errors: no constructor that takes an int, no
    overloaded operator(), no norm function.
    Post the smallest compilable (with the exception of the error you are
    getting) code that exhibits your problem.

    Dan


    <> wrote in message
    news:...
    > Ok here it is:
    > template <typename T>
    > class Vector
    > {
    > private:
    > int _size;
    > void VecAlloc(int size)
    > {
    > }
    > public:
    > template<typename Other> Vector(const Vector<Other>& Vin)
    > {
    > _size=Vin.size();
    > VecAlloc(Vin.size());
    > vassign(Vin);
    > }
    > ....
    > template<typename Other> void vassign( const Vector<Other>& Vin);
    > ....
    > };
    > template<typename T>
    > template<typename Other> void Vector<T>::vassign(const Vector<Other>&
    > Vin)
    > {
    > for (int i=0;i<Vin.size();i++)
    > (*this)(i)=Vin(i);
    > }
    >
    > template<> template<typename ComplexSingle> void
    > Vector<float>::vassign(const Vector<ComplexSingle>& Vin)
    > {
    > for (int i=0;i<Vin.size();i++)
    > (*this)(i)=norm(Vin(i));
    > }
    >
    > ....
    > int main (void)
    > {
    >
    > Vector<double> V(5);
    > Vector<float>V1(V); //compile errror
    > //instantiated from `Vector<T>::Vector(const Vector<Other>&) [with
    > Other = double, T = float]'
    > //test2.cc:11: instantiated from here
    > //matlib2.h:290: error: no matching function for call to `norm(double)'
    >
    > return 1;
    > }
    >
     
    Dan Cernat, Jul 20, 2005
    #6
  7. Guest

    I think the problem is that my specialized function takes precedence
    over my generic function and gets called by the compiler instead.
     
    , Jul 20, 2005
    #7
    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. Dave
    Replies:
    4
    Views:
    7,771
    pdixtl
    Jun 4, 2010
  2. Ruben Campos
    Replies:
    3
    Views:
    6,531
  3. BigMan
    Replies:
    1
    Views:
    539
  4. Joseph Turian
    Replies:
    4
    Views:
    622
    John Carson
    Mar 20, 2006
  5. toton
    Replies:
    2
    Views:
    360
    toton
    Feb 22, 2008
Loading...

Share This Page