473,320 Members | 2,111 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,320 software developers and data experts.

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 1592
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.com> wrote in message
news:MP************************@news.cc.tut.fi
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.net.au> wrote in message
news:dj***********@otis.netspace.net.au
"Kalle Rutanen" <no**@here.com> wrote in message
news:MP************************@news.cc.tut.fi
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 imageformatconvert.h and imageformatconvert.inl. There (.h) I
had to explicitly specialize the ImageFormatConvert 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 imageformatconvert.h and imageformatconvert.inl. There (.h) I
had to explicitly specialize the ImageFormatConvert 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_conversion {
typedef default_tag tag;
};

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

// do type specific stuff
f_(typename select_conversion<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
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. ...
12
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...
6
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
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...
2
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...
6
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...
6
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
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
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
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;...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.