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



 DEVELOP > c-Plus-Plus > unexpected compile error w/ member function specialization of a template class

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 19 Jul 2005 04:33:23 PM
Object: unexpected compile error w/ member function specialization of a template class
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,
.

User: "Victor Bazarov"

Title: Re: unexpected compile error w/ member function specialization ofa template class 19 Jul 2005 05:03:11 PM
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
.
User: ""

Title: Re: unexpected compile error w/ member function specialization of a template class 19 Jul 2005 05:35:34 PM
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.
.
User: "Victor Bazarov"

Title: Re: unexpected compile error w/ member function specialization ofa template class 19 Jul 2005 05:47:14 PM
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
.

User: ""

Title: Re: unexpected compile error w/ member function specialization of a template class 19 Jul 2005 05:48:49 PM
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;
}
.
User: "Dan Cernat"

Title: Re: unexpected compile error w/ member function specialization of a template class 19 Jul 2005 06:12:12 PM
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
<bluekite2000@gmail.com> wrote in message
news:1121813329.007439.7790@f14g2000cwb.googlegroups.com...

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

.
User: ""

Title: Re: unexpected compile error w/ member function specialization of a template class 19 Jul 2005 06:36:45 PM
I think the problem is that my specialized function takes precedence
over my generic function and gets called by the compiler instead.
.






  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER