Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   specialized member function takes precedence over generic template member function (http://www.velocityreviews.com/forums/t447219-specialized-member-function-takes-precedence-over-generic-template-member-function.html)

bluekite2000@gmail.com 07-20-2005 08:35 PM

specialized member function takes precedence over generic template member function
 
Here is the compilable code, along w/ the error

#include<iostream>
#include<complex>
typedef std::complex<float> ComplexSingle;
using namespace std;
template<typename T> class X
{
private:
T number;
public:
X(T value)
{
number=value;
}
template<typename Other> X(Other Y)
{
assign(Y);
}
template<typename Other> void assign(Other Y);


T return_number()
{
return number;
}
};

template<typename T>
template<typename Other> void X<T>::assign( Other Y)
{
number=Y.return_number();
}

template<> template<typename ComplexSingle> void
X<float>::assign(ComplexSingle Y)
{
number=norm(Y.return_number());
}
int main (void)
{
//this works fine
ComplexSingle a(2,3);
X<ComplexSingle> A(a);
X<float> B(A);


//error
//In member function `void X<T>::assign(Other) [with Other =
//X<double>, T = float]':
//ex1.cc:16: instantiated from `X<T>::X(Other) [with Other =
// X<double>, T = float]'
//ex1.cc:45: instantiated from here
//ex1.cc:35: error: no matching function for call to `norm(double)'

X<double> C(3.3);
X<float> D(C);
return 0;
}


Victor Bazarov 07-20-2005 08:58 PM

Re: specialized member function takes precedence over generic templatemember function
 
bluekite2000@gmail.com wrote:
> Here is the compilable code, along w/ the error
>
> #include<complex>


> template<typename T> class X
> {
> T number;
> public:
> [..]
> T return_number()
> {
> return number;
> }
> };
>
> template<> template<typename ComplexSingle> void
> X<float>::assign(ComplexSingle Y)
> {
> number=norm(Y.return_number());


'norm' is a template declared for all complex<T>. When you call it like
that with 'Y' as 'X<double>', it looks around and cannot find any 'norm'
for 'double', then it sees that there is 'norm<T>(const complex<T>&)',
but since the argument you give is 'double', it can't understand what 'T'
should be. This is not one of contexts from which 'T' can be deduced.

What are you trying to achieve, anyway? I can only recommend defining
your own 'norm':

double norm(double a) {
return fabs(a); // or whatever
}

> }


V


All times are GMT. The time now is 01:23 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57