By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,923 Members | 1,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,923 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
bl**********@gmail.com 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
Jul 23 '05 #2

P: n/a
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 23 '05 #3

P: n/a
bl**********@gmail.com 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
Jul 23 '05 #4

P: n/a
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 23 '05 #5

P: n/a
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
<bl**********@gmail.com> wrote in message
news:11********************@f14g2000cwb.googlegrou ps.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;
}

Jul 23 '05 #6

P: n/a
I think the problem is that my specialized function takes precedence
over my generic function and gets called by the compiler instead.

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.