473,850 Members | 2,073 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

specialized member function takes precedence over generic template member function

Here is the compilable code, along w/ the error

#include<iostre am>
#include<comple x>
typedef std::complex<fl oat> ComplexSingle;
using namespace std;
template<typena me T> class X
{
private:
T number;
public:
X(T value)
{
number=value;
}
template<typena me Other> X(Other Y)
{
assign(Y);
}
template<typena me Other> void assign(Other Y);
T return_number()
{
return number;
}
};

template<typena me T>
template<typena me Other> void X<T>::assign( Other Y)
{
number=Y.return _number();
}

template<> template<typena me ComplexSingle> void
X<float>::assig n(ComplexSingle Y)
{
number=norm(Y.r eturn_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(Ot her) [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 1637
bl**********@gm ail.com wrote:
Here is the compilable code, along w/ the error

#include<comple x> template<typena me T> class X
{
T number;
public:
[..]
T return_number()
{
return number;
}
};

template<> template<typena me ComplexSingle> void
X<float>::assig n(ComplexSingle Y)
{
number=norm(Y.r eturn_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
1883
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 zip. Anyhow, after much trial and error I came up with this. This "contains_member" template sets value to 1 if the type D contains a typedef named MEMBER of type TD.
4
2454
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 far is pretty simple: using an idea of Andrei Alexandrescu (at least that's where I first read it) to create a lightweight way of overloading member functions since member function specialization is not possible.
1
1359
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 agree on the types for the methods. In the code below, the constructor to NLF2 (near the bottom) takes as its last two arguments two function pointers of type void(*)(...), and apparently &PR<2>::PR_init is of type void(PR<2>::*)(...). If I...
9
2325
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 same type for a given instance of the class, but different types for different instances.) #include<set> using namespace std; template<class T>
2
3731
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
1821
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 boost::call_traits<T>::param_type param_type; typedef typename boost::call_traits<T>::reference reference;
2
1877
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 boost::call_traits<T>::param_type param_type; .... public:
6
2903
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 My<int; What if I have a template class template <typename T, typename Uclass My ;
13
6611
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 trying to use from an executable, compile using gcc 4.1.2. Everything works fine until I try specializing one of the static member function templates in a non-template class. I have a feeling I'm messing up something obvious so before I post a...
0
9895
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9741
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11011
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10666
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10724
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7071
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5929
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4140
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3178
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.