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

B

bluekite2000

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,
 
V

Victor Bazarov

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
 
B

bluekite2000

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.
 
V

Victor Bazarov

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
 
B

bluekite2000

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

Dan Cernat

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
 
B

bluekite2000

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top