Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > unexpected compile error w/ member function specialization of a template class

Reply
Thread Tools

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

 
 
bluekite2000@gmail.com
Guest
Posts: n/a
 
      07-19-2005
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,

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-19-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
bluekite2000@gmail.com
Guest
Posts: n/a
 
      07-19-2005
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.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-19-2005
(E-Mail Removed) 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
 
Reply With Quote
 
bluekite2000@gmail.com
Guest
Posts: n/a
 
      07-19-2005
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;
}

 
Reply With Quote
 
Dan Cernat
Guest
Posts: n/a
 
      07-19-2005
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


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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;
> }
>



 
Reply With Quote
 
bluekite2000@gmail.com
Guest
Posts: n/a
 
      07-19-2005
I think the problem is that my specialized function takes precedence
over my generic function and gets called by the compiler instead.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Full specialization of a member function template of a class template Dave C++ 4 06-04-2010 12:15 PM
Specialization of member function template in template class? Joseph Turian C++ 4 03-20-2006 10:07 AM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Out-of-class-template member template specialization syntax BigMan C++ 1 03-26-2005 02:23 PM
Member template function specialization in a template class Ruben Campos C++ 3 02-15-2005 10:40 AM



Advertisments