473,382 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

Jul 23 '05 #1
1 1602
bl**********@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
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gianni Mariani | last post by:
I remember seeing a neat template specialization trick posted here a few months ago that allowed the detection of a type containing a member. After a day searching through google archives I come up...
4
by: Justin Miller | last post by:
Ok, I tried to make that subject as descriptive as possible. What I'm trying to do: I'm attempting to use policies to create a generic memento (design pattern) template. My Memento template so...
1
by: Martin Magnusson | last post by:
I have a partially specialized templated class, which at one point needs to pass pointers to some of its methods to another function. However, it seems that my compiler (gcc) and me don't quite...
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
2
by: Aarti | last post by:
Say I have a class template as follows template<typename T> class foo { public: void test(); }; template<typename T>
4
by: Olaf | last post by:
Hi, I'm writing a small wrapper for libcurl. For Options I use: template<typename T, long CURLOPT_ID> class CurlOption: boost::noncopyable { public: typedef typename...
2
by: Olaf | last post by:
Hi, I'm working on a thin libcurl layer. Therefore I have the following classes: template<typename T, long CURLOPT_ID> class CurlOption: boost::noncopyable { public: typedef typename...
6
by: year1943 | last post by:
For template <typename Tclass My ; I can define partial spec-ns somewhat like template <typename Tclass My<T*; or template <typename Tclass My<Another<T ; And full spec-n, say template <class...
13
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.