473,657 Members | 2,574 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help partial sepecialization

Hi there,
I am learning template programming. When testing the partial
specialization, I have some probelms

Here is a full templated class
template <typename T, int n>
class CT
{
public:
T data[n][100][100];

CT() {...}

const T& get(int k, int i, int j)
{
return data[k][i][j];
}
};

I have a partial specialization verion as follow

template <typename T>
class CT<T, 1>
{
public:
T data[1][100][100];

const T& get(int i, int j)
{
return data[0][i][j];
}
};

In main, I have

int main(void)
{
CT<double, 2> ct1;
CT<double, 1> ct2;

cout << ct1.get(1, 5, 8) << endl; // OK! return what I want
cout << ct2.get(5, 8) << endl; // also OK
cout << ct2.get(0, 5, 8) << endl; // ERROR !!!
}

I wonder how this error come ! Both ct1 and ct2 come from a same class
except the different template parameter, why compiler complian no
get(k, i, j) is defined in partial specialization class? If I want a
partial specialization class with all members available, should I
rewrite all the code ?

Thanks in advance

Dec 30 '05 #1
4 1493

wa***@wakun.com wrote:
Hi there,
I am learning template programming. When testing the partial
specialization, I have some probelms

Here is a full templated class
template <typename T, int n>
class CT
{
public:
T data[n][100][100];

CT() {...}

const T& get(int k, int i, int j)
{
return data[k][i][j];
}
};

I have a partial specialization verion as follow

template <typename T>
class CT<T, 1>
{
public:
T data[1][100][100];

const T& get(int i, int j)
{
return data[0][i][j];
}
};

In main, I have

int main(void)
{
CT<double, 2> ct1;
CT<double, 1> ct2;

cout << ct1.get(1, 5, 8) << endl; // OK! return what I want
cout << ct2.get(5, 8) << endl; // also OK
cout << ct2.get(0, 5, 8) << endl; // ERROR !!!
}

I wonder how this error come ! Both ct1 and ct2 come from a same class
except the different template parameter, why compiler complian no
get(k, i, j) is defined in partial specialization class? If I want a
partial specialization class with all members available, should I
rewrite all the code ?

Thanks in advance
They are different classes; in fact very differentcout << ct2.get(0, 5, 8) << endl; // ERROR !!!

you specialized class doesn't have an overload of this method that
takes 3 params.
look at faq for more clarification

Dec 30 '05 #2
wa***@wakun.com wrote:
Hi there,
I am learning template programming. When testing the partial
specialization, I have some probelms
If I want a
partial specialization class with all members available, should I
rewrite all the code ?


Yes.
Jonathan

Dec 30 '05 #3

Jonathan Mcdougall wrote:
wa***@wakun.com wrote:
Hi there,
I am learning template programming. When testing the partial
specialization, I have some probelms
If I want a
partial specialization class with all members available, should I
rewrite all the code ?


Yes.

OK. This time I consider inheritance

template <typename T, int n>
class Base
{
public:
T data[n][100][100];

Base() {...}

void show(void)
{
cout << "Hi!" << endl;
}

const T& get(int k, int i, int j)
{
return data[k][i][j];
}

const T& get2(int k, int i, int j)
{
return data[k][i][j];
}

};

template <typename T, int n>
class CT : public Base<T, n>
{
public:
CT() :Base<T, n> {...}
};

template <typename T>
class CT<T, 1> : public Base<T, 1>
{
public:

CT<T,1>() :Base<T, 1> {...}

const T& get2(int i, int j)
{
return data[0][i][j];
}

};

// main
int main(void)
{
CT<double, 2> ct1;
CT<double, 1> ct2;

cout << ct1.get(1, 2, 3) << endl; // OK, of course
cout << ct1.show() << endl; // OK, of course

cout << ct2.show() << endl; // No problem, show is defined
in Base
cout << ct2.get(0, 1, 2) << endl; // OK. get is kept in both
CT<double, n> and CT<double, 1>
cout << ct2.get2(3, 2) << endl; // OK. overload function
ctou << ct2.get2(0, 1, 2) << endl; // ERROR!!! No such function!?
return 0;
}

Why compiler cannot find get2?

Dec 31 '05 #4
wa***@wakun.com wrote:
Hi there,
I am learning template programming. When testing the partial
specialization, I have some probelms

I wonder how this error come ! Both ct1 and ct2 come from a same class
except the different template parameter, why compiler complian no
get(k, i, j) is defined in partial specialization class? If I want a
partial specialization class with all members available, should I
rewrite all the code ?


While you certainly have the partially-specialized class template be
more-or-less a duplicate of the general class template, doing so
creates a maintenance hassle, since the two class templates need to be
maintained in parallel.

A simpler approach is to declare the get method that accepts three
parameters in the general template, but provide an implementation only
in a full specialization:

#include <iostream>

using std::cout;
using std::endl;

template <class T, int N>
class CT
{
public:
T data[N][100][100];

CT() {}

const T& get(int k, int i, int j)
{
return data[k][i][j];
}

const T& get(int i, int j); // not implemented here
};

template <>
const double& CT<double, 1>::get(int i, int j)
{
return data[0][i][j];
}

int main()
{
CT<double, 2> ct1;
CT<double, 1> ct2;

cout << ct1.get(1, 5, 8) << endl; // OK! return what I want
cout << ct2.get(5, 8) << endl; // also OK
cout << ct2.get(0, 5, 8) << endl; // OK
}

Greg

Dec 31 '05 #5

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

Similar topics

7
2653
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask it differently. FOUR QUESTIONS: The background: I got three (3) files
4
1838
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it elaborate to make sure my questions are clear enough. Let's say I need to write a function whose logic is same for all types (T) except in the case of T * (including const T *). Furtheremore , the function needs to be written differently for...
9
2518
by: Gomaw Beoyr | last post by:
Two question about the "partial classes" (in the next wersion of ..NET). Question 1 ========== Will partial classes (in the next version of C#) have to be declared "partial" in ALL places. I.e. do we have to need to write:
16
2637
by: pawel.pabich | last post by:
Hajo, I would like to have 2 my own partial classes. For example: Default.aspx.cs Default2.aspx.cs and they both will relate to Default.aspx page.
10
2429
by: ptass | last post by:
Hi In asp.net 2.0 an aspx files .cs file is a partial class and all works fine, however, I thought I’d be able to create another class file, call it a partial class and have that compile and load as a 3rd partial class. This would be handy so i can generate standard code into one of the partial classes, while having my custom code untouched
1
2939
by: chungiemo | last post by:
Hi thought I would do another thread as this one is a bit different from the previous problem I am looking for a solution to the relating problem Comparing 2 access databases with 2 tables, they are non-related tables, but should have been related by the ID, and reason for this was due to the poor data entry standards. I am trying to compare both databases with a Partial ID in Table 1 and match it to the Full ID in Table 2 with the...
5
1977
by: SAL | last post by:
Hello, I would like to be able to set the WHERE clause of a select statement on the fly. I have a DataAccess layer designed using the DataSet designer and a BusinessLogic layer using classes. The business logic layer is decorated using the <System.ComponentModel.DataObject()> attributes and they are used to bind to controls and Object datasets on web forms. Is there a resonable way to set the WHERE clause on the fly with a partial class...
10
1520
by: JDeats | last post by:
So I have a class that spans over two partial classes in code, here's an example (do not read much into this, the code is of no practical use, this is just a simple example of where my confusion occurs). // Inside SharedClassExample1.cs public partial class SharedClassExample { public List<stringBooksOnShelf { get; set; } public List<stringBooksOnDesk { get; set; }
2
10023
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name is used to access and read the value stored in it C.A variable is allocated or deallocated in memory during runtime D.A variable can be initialized at the time of its creation or later 2. The.……types feature facilitates the definition of classes...
0
8305
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
8823
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
8730
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...
0
7321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1607
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.