C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

specialized member function unexpectedly hides generic templ

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
bluekite2000@gmail.com
Guest





PostPosted: Thu Jul 21, 2005 10:37 am    Post subject: specialized member function unexpectedly hides generic templ Reply with quote



Here is the code:

#include <iostream>
#include <complex>

typedef std::complex<float> ComplexSingle;


template <typename T>
class Vector
{
private:
int _size;
T* _vp;
void VecAlloc(int size)
{
_vp=new T[size];
}
public:
Vector()
{
}
Vector(int size)
:_size(size)
{
VecAlloc(size);
}
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);

T& operator () (int pos)
{
return _vp[pos];
}
T operator () (int pos) const
{
return _vp[pos];
}
int size() const
{
return _size;
}
~Vector()
{
}
};


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)=std::norm(Vin(i));
}

int main (void)
{
Vector Vector<float>V2(V1);//Specialized function is called here.
Vector<double> V3(5);
Vector<float>V4(V3);//Compile error!
// instantiated from `Vector<T>::Vector(const Vector<Other>&) [with
//Other = double, T = float]'
//ex.cc:72: instantiated from here
//ex.cc:64: error: no matching function for call to `norm(double)'

return 0;
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Greg
Guest





PostPosted: Fri Jul 22, 2005 11:03 am    Post subject: Re: specialized member function unexpectedly hides generic t Reply with quote



[email]bluekite2000 (AT) gmail (DOT) com[/email] wrote:
Quote:
Here is the code:

#include <iostream
#include
typedef std::complex

template <typename T
class Vector
{
...
template
{
_size=Vin.size();
VecAlloc(Vin.size());
vassign(Vin);

}
template<typename Other> void vassign( const Vector<Other>& Vin);

....
~Vector()
{
}
};


template<typename T
template 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)=std::norm(Vin(i));
}

int main (void)
{
Vector Vector<float>V2(V1);//Specialized function is called here.
Vector<double> V3(5);
Vector<float>V4(V3);//Compile error!
// instantiated from `Vector<T>::Vector(const Vector<Other>&) [with
//Other = double, T = float]'
//ex.cc:72: instantiated from here
//ex.cc:64: error: no matching function for call to `norm(double)'

return 0;
}


The problem here is that the specialized template function is not so
specialized. Specifically, examine this declaration closely,
particularly the second line:


template<>
template<typename ComplexSingle>
void Vector<float>::vassign(const Vector<ComplexSingle>& Vin)


"ComplexSingle" in this declaration is the name of the type parameter
for the template instantiation, it is not the name of the ComplexSingle
class declared above. The template type parameter's name happens to
have the same name as a declared class, but the names of template type
parameters do not conflict with identically named class types.

The fix is to eliminate the inadvertently-named template type parameter
and declare the specialized function as intended:

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

Greg


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.