473,769 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Templates, specialization

Here is a short code. I can't see why the template specialization does
not work (vsnet2003)? Does the standard allow these kind of
specializations ?

template <int i>
class B {};

template <typename T>
class A
{
public:
void f();
};

template <typename T>
void A<T>::f()
{
}

template <int i>
void A<B<i> >::f()
{
}

int main()
{
A<B<2> > a;
a.f();

return 0;
}

--
http://kaba.hilvi.org
Oct 25 '05 #1
6 1625
Kalle Rutanen wrote:
Here is a short code. I can't see why the template specialization does
not work (vsnet2003)? Does the standard allow these kind of
specializations ?

template <int i>
class B {};

template <typename T>
class A
{
public:
void f();
};

template <typename T>
void A<T>::f()
{
}

template <int i>
void A<B<i> >::f()
{
}

int main()
{
A<B<2> > a;
a.f();

return 0;
}


You're basically trying to partially specialize a function now; that
won't work. It does work however if you partially specialize the class,
and then define the member function out of the class. Ie. add the
following few lines:

template <int i>
class A<B<i> > {
public:
void f();
};
--
Regards,

Ferdi Smit
Email: Fe********@cwi. nl
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
Oct 25 '05 #2
"Kalle Rutanen" <no**@here.co m> wrote in message
news:MP******** *************** *@news.cc.tut.f i
Here is a short code. I can't see why the template specialization does
not work (vsnet2003)? Does the standard allow these kind of
specializations ?
No, it doesn't.
template <int i>
class B {};

template <typename T>
class A
{
public:
void f();
};
So A has a type parameter.
template <typename T>
void A<T>::f()
{
}

template <int i>
void A<B<i> >::f()
{
}
Now, you are trying to give it a non-type parameter. This isn't really
specialization at all. It is a change in the nature of the template from one
having a type parameter to one having a non-type parameter.
int main()
{
A<B<2> > a;
a.f();

return 0;
}


Your example doesn't give much indication what you are trying to achieve.
Making a guess, something resembling what you want might be achieved
with partial specialisation, as follows:

#include <iostream>
using namespace std;

template <int i>
class B {};

template <typename T, int i=0>
class A
{
public:
void f();
};

template <typename T, int i>
void A<T,i>::f()
{
cout << "Member function of general class\n";
}

// We can't partially specialize a function,
// only a class, so we partially specialize the class
// to depend on int only

template <int i>
class A<B<i> >
{
public:
void f();
};

// Now we can define the member function of the partially
// specialized class.
template <int i>
void A<B<i> >::f()
{
cout << "Member function of partially specialized class\n";
}

int main()
{
A<char *> a1;
A<B<2> > a2;
a1.f();
a2.f();

return 0;
}
--
John Carson

Oct 25 '05 #3
"Ferdi Smit" <Fe********@cwi .nl> wrote in message news:Io******** @cwi.nl
Kalle Rutanen wrote:
Here is a short code. I can't see why the template specialization
does not work (vsnet2003)? Does the standard allow these kind of
specializations ?

template <int i>
class B {};

template <typename T>
class A
{
public:
void f();
};

template <typename T>
void A<T>::f()
{
}

template <int i>
void A<B<i> >::f()
{
}

int main()
{
A<B<2> > a;
a.f();

return 0;
}


You're basically trying to partially specialize a function now; that
won't work. It does work however if you partially specialize the
class, and then define the member function out of the class. Ie. add
the following few lines:

template <int i>
class A<B<i> > {
public:
void f();
};


Interesting code. I didn't realise it was possible. You appear to have
partially specialized class A to depend only on int when it didn't depend on
int to begin with --- at least not necessarily. Nevertheless, your code
plainly works and I see, upon checking, that Vandevoorde and Josuttis (C++
Templates) give an example (p. 351) where partial specialization involves
the introduction of a new parameter. Good to know. Thank you.
--
John Carson

Oct 25 '05 #4
"John Carson" <jc************ ****@netspace.n et.au> wrote in message
news:dj******** ***@otis.netspa ce.net.au
"Kalle Rutanen" <no**@here.co m> wrote in message
news:MP******** *************** *@news.cc.tut.f i
template <int i>
class B {};

template <typename T>
class A
{
public:
void f();
};


So A has a type parameter.
template <typename T>
void A<T>::f()
{
}

template <int i>
void A<B<i> >::f()
{
}


Now, you are trying to give it a non-type parameter. This isn't really
specialization at all. It is a change in the nature of the template
from one having a type parameter to one having a non-type parameter.


From Ferdi's post, I have now learned that this type of switch is indeed
possible for classes, though not for functions since it counts as a form of
partial specialization. I stand corrected.

--
John Carson

Oct 25 '05 #5
Hello, John and Ferdi.

It is indeed possible to specialize the class in the way Ferdi
mentioned. I am surprised too see the given situation really is
forbidden. "C++ in a nutshell", page 195: "You cannot partially
specialize a member of a class template."

But we can still explicitly specialize:

template <>
void A<B<1> >::f()
{
}

Can you think of a reason why this decision was made?

My intention is to convert pixel color formats to each other using
template meta programming (for example ARGB8888 <-> ARGB1555). You can
see the actual code here:

http://kaba.hilvi.org/project/convert/

Look at imageformatconv ert.h and imageformatconv ert.inl. There (.h) I
had to explicitly specialize the ImageFormatConv ert template class three
times (like Ferdi suggested), while the code for them is practically
identical.

--
http://kaba.hilvi.org
Oct 25 '05 #6
Kalle Rutanen wrote:
Hello, John and Ferdi.

It is indeed possible to specialize the class in the way Ferdi
mentioned. I am surprised too see the given situation really is
forbidden. "C++ in a nutshell", page 195: "You cannot partially
specialize a member of a class template."

But we can still explicitly specialize:

template <>
void A<B<1> >::f()
{
}

Can you think of a reason why this decision was made?
You can't partially specialize functions. Instead you have overloading,
which is very powerful. Just remember: functions -> overloading, class
-> specialization. (Explicitly) specializing functions can also cause
some troubles when overloading is used as well. Prefer overloading.

My intention is to convert pixel color formats to each other using
template meta programming (for example ARGB8888 <-> ARGB1555). You can
see the actual code here:

http://kaba.hilvi.org/project/convert/

Look at imageformatconv ert.h and imageformatconv ert.inl. There (.h) I
had to explicitly specialize the ImageFormatConv ert template class three
times (like Ferdi suggested), while the code for them is practically
identical.


You could use overloading with a tag type in your original example.

Ie. something like this (untested, top of my head):

template <int i>
struct B{};

struct default_tag {};
struct special_tag {};

template <typename T>
struct select_conversi on {
typedef default_tag tag;
};

template <int i>
struct select_conversi on<B<i> > {
typedef special_tag tag;
};
template <typename T>
class A {
public:
void f() {
// do common stuff

// do type specific stuff
f_(typename select_conversi on<T>::tag());
}
private:
// select on overloading tag type
void f_(const default_tag& x) {}
void f_(const special_tag& x) {}
};
A<int> ai;
A<B<2> > ab;
ai.f();
ab.f();

Or something similar.

--
Regards,

Ferdi Smit (M.Sc.)
Email: Fe********@cwi. nl
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
Oct 26 '05 #7

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

Similar topics

4
6486
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. Note that the overload is identical to the specialization except, of course, for the missing "template <>". I don't know if my questions will be a bit too broad or not, but I thought I'd give it shot... When is overloading preferable to...
12
5260
by: Simon | last post by:
Hi, I'm having a problem with templates and specialisation. I'm using it to overload the same function so it can return different things. I can't see what I'm doing wrong, although my compiler promises me I am! Here follows an example of my code, and i've included the error VC is giving me if that helps. Can anyone point me in the right direction? Cheers, Simon ;o) --
6
2643
by: jesse | last post by:
I am frustrated by class specialization. i don't think it helps me a lot. suppose we have template <class T> class Talkative { T& t; public:
4
2587
by: SainTiss | last post by:
Hi, From what I've read in several places, it seems that explicit specialization of member functions of class templates is allowed, but partial specialization isn't: template<class T, class R> class A { void foo(); }
2
1508
by: Shekhar | last post by:
template<typename T> struct A{}; //line 1 template<typename T> struct B{}; //line 2 template<typename T> struct B<A<T> > {}; //line 3: partial specialization of B VC6.0 compiler results for the above: at line 3: error C2989: 'B<struct A<T> >' : template class has already been defined as a non-template class
6
1816
by: Matt Taylor | last post by:
I'm trying to write an x86 assembler in C++ for use in a debugger. What I'd like do is to use template specialization to prevent invalid combinations from compiling. Thus one could not accidentally add a 16-bit register and an 8-bit register since there is no encoding for this on the x86 architecture. My trouble has stemmed from the fact that enumerators are only integers and can be freely cast into other enumerators, and I was using them...
6
2236
by: vch | last post by:
When defining templates, can I cound on the compiler to parse only those templates that are actually used? For example, in the following definitions: <code> template <class T> struct Conv {
16
16290
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
11
1825
by: Peter Oliphant | last post by:
Is there any plan to support templates with managed code in the (near) future? For instance, VS.NET 2005... : )
9
2664
by: Jerome Durand | last post by:
Hello, I'm trying to write something along the following lines but I cannot get this to compile. template <typename derivedstruct Base { typedef typename derived::valueType valueType; virtual valueType Value() = 0; };
0
9579
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
9422
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
10038
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
9987
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
8867
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
7404
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
5294
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...
0
5444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2812
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.