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

Specialization of Member functions

Please take a look at the following code snippit:
-------------------------------------------------------
template<class TraitClass=int>
class TestClass
{
public:
void CallFunc()
{
SpecializedFunct_<TraitClass>();
}

private:
template<class TraitClass >
void SpecializedFunct_(void)
{
// called for non-specialized
}

template<>
void SpecializedFunct_<int>(void)
{
// specialized for TraitClass=int
}

template<>
void SpecializedFunct_<char>(void)
{
// specialized for TraitClass=char
}
};
-------------------------------------------------------

It works GREAT with Visual Studio .NET 2003. Complains to high-heaven with
the gcc 3.2.2 compiler that came with Red Hat Linux 9.0.

I have done some digging and found a few blurbs in my google-mining that
says something to the effect that specialization of member functions for a
non-specialized class is not supported.

So I have 2 questions:

1. Has the standard changed and MS is on the ball, or is it a MS compiler
extension that allows this to work?

2. Would greatly appreciate suggestions on how to get around this issue, I
really really would like to have specialized member functions.

thanx!
Jul 22 '05 #1
9 1588
Patrick Kutch wrote:
Please take a look at the following code snippit:
-------------------------------------------------------
template<class TraitClass=int>
class TestClass
{
public:
void CallFunc()
{
SpecializedFunct_<TraitClass>();
}

private:
template<class TraitClass >
void SpecializedFunct_(void)
{
// called for non-specialized
}

template<>
void SpecializedFunct_<int>(void)
{
// specialized for TraitClass=int
}

template<>
void SpecializedFunct_<char>(void)
{
// specialized for TraitClass=char
}
};
-------------------------------------------------------

It works GREAT with Visual Studio .NET 2003. Complains to high-heaven with
the gcc 3.2.2 compiler that came with Red Hat Linux 9.0.

I have done some digging and found a few blurbs in my google-mining that
says something to the effect that specialization of member functions for a
non-specialized class is not supported.

So I have 2 questions:

1. Has the standard changed and MS is on the ball, or is it a MS compiler
extension that allows this to work?

2. Would greatly appreciate suggestions on how to get around this issue, I
really really would like to have specialized member functions.

thanx!


Would this work for you?
template<class TraitClass=int>
class TestClass
{
public:
void CallFunc()
{
SpecializedFunct_();
}

private:
void SpecializedFunct_()
{
// called for non-specialized
}
};

template<>
void
TestClass<int>::SpecializedFunct_(void)
{
// specialized for TraitClass=int
}

template<>
void
TestClass<char>::SpecializedFunct_(void)
{
// specialized for TraitClass=char
}
-Slawomir Lisznianski
Jul 22 '05 #2

"Patrick Kutch" <Pa*****@dsl-only.net> wrote in message
news:40********@nntp0.pdx.net...
Please take a look at the following code snippit:
-------------------------------------------------------
template<class TraitClass=int>
class TestClass
{
public:
void CallFunc()
{
SpecializedFunct_<TraitClass>();
}

private:
template<class TraitClass >
void SpecializedFunct_(void)
{
// called for non-specialized
}

template<>
void SpecializedFunct_<int>(void)
{
// specialized for TraitClass=int
}

template<>
void SpecializedFunct_<char>(void)
{
// specialized for TraitClass=char
}
};
-------------------------------------------------------

It works GREAT with Visual Studio .NET 2003. Complains to high-heaven with
the gcc 3.2.2 compiler that came with Red Hat Linux 9.0.

I have done some digging and found a few blurbs in my google-mining that
says something to the effect that specialization of member functions for a
non-specialized class is not supported.

So I have 2 questions:

1. Has the standard changed and MS is on the ball, or is it a MS compiler
extension that allows this to work?
I would place my trust with gcc here. I don't think explicit specialization
should work in your case.
2. Would greatly appreciate suggestions on how to get around this issue, I
really really would like to have specialized member functions.


Slawomir has suggested a solution which could work for you.
He specializes the class rather than the member function (Nice way to look at
things differently ;-))

Best wishes,
Sharad

Jul 22 '05 #3

"Slawomir Lisznianski" <sl**********@asyncnet.com> wrote in message
news:b5********************@dls.net...
Patrick Kutch wrote:
Please take a look at the following code snippit:
-------------------------------------------------------
template<class TraitClass=int>
class TestClass
{
public:
void CallFunc()
{
SpecializedFunct_<TraitClass>();
}

private:
template<class TraitClass >
void SpecializedFunct_(void)
{
// called for non-specialized
}

template<>
void SpecializedFunct_<int>(void)
{
// specialized for TraitClass=int
}

template<>
void SpecializedFunct_<char>(void)
{
// specialized for TraitClass=char
}
};
-------------------------------------------------------

It works GREAT with Visual Studio .NET 2003. Complains to high-heaven with the gcc 3.2.2 compiler that came with Red Hat Linux 9.0.

I have done some digging and found a few blurbs in my google-mining that
says something to the effect that specialization of member functions for a non-specialized class is not supported.

So I have 2 questions:

1. Has the standard changed and MS is on the ball, or is it a MS compiler extension that allows this to work?

2. Would greatly appreciate suggestions on how to get around this issue, I really really would like to have specialized member functions.

thanx!


Would this work for you?
template<class TraitClass=int>
class TestClass
{
public:
void CallFunc()
{
SpecializedFunct_();
}

private:
void SpecializedFunct_()
{
// called for non-specialized
}
};

template<>
void
TestClass<int>::SpecializedFunct_(void)
{
// specialized for TraitClass=int
}

template<>
void
TestClass<char>::SpecializedFunct_(void)
{
// specialized for TraitClass=char
}
-Slawomir Lisznianski

I don't believe so. The example is trivial, my actual class has 6 template
parameters - would be rather difficult to specialize all of them.

- Patrick
Jul 22 '05 #4
Patrick Kutch wrote in news:40********@nntp0.pdx.net:
Please take a look at the following code snippit:
-------------------------------------------------------
template<class TraitClass=int>
class TestClass
{
public:
void CallFunc()
{
This is suspect, why wre you passing in the current template's
paramiter, members are already "specialized" on this paramiter.
SpecializedFunct_<TraitClass>();
}

private:
template<class TraitClass >
void SpecializedFunct_(void)
{
// called for non-specialized
}

template<>
void SpecializedFunct_<int>(void)
{
// specialized for TraitClass=int
}

template<>
void SpecializedFunct_<char>(void)
{
// specialized for TraitClass=char
}
};
-------------------------------------------------------

It works GREAT with Visual Studio .NET 2003. Complains to high-heaven
with the gcc 3.2.2 compiler that came with Red Hat Linux 9.0.

I got it to compile with CBUilderX (has an EDG frontend) also.
I have done some digging and found a few blurbs in my google-mining
that says something to the effect that specialization of member
functions for a non-specialized class is not supported.

So I have 2 questions:

1. Has the standard changed and MS is on the ball, or is it a MS
compiler extension that allows this to work?

Nope, but its quite common for compilers to get this "cutting edge"
stuff wrong. Which is wrong though I don't know, though in my brief
scan of the standard I was unable to find anything excluding them.

Note that there is no way of defining these specialization's outside
the of the class template, so *if* legal they have to be "inline".
2. Would greatly appreciate suggestions on how to get around this
issue, I really really would like to have specialized member
functions.

The usual workaround is an extra level of indirection:

template < typename T > class TestClass; // forward

template < typename T >
struct SpecializedMember_helper
{
template < typename U >
static void apply( TestClass< U > &that )
{
//non specilaized functionality
}
};

template < >
struct SpecializedMember_helper< int >
{
template < typename U >
static void apply( TestClass< U > &that )
{
//int specilaized functionality
}
};
template < typename Traits >
class TestClass
{
//...
template < typename T >
void SpecializedMember_()
{
SpecializedMember_helper< T >::apply( *this );
}
};

This also works for partial specialization's.

If T always equals U in your real code (as it did in your example)
then SpecializedMember_helper<T>::apply() doesn't need to be a
template.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #5
Thanx to Rob and others for the assistance. When I move to a non-trivial
class, bad things still happen under Linux. Please take a look at the
following code:
-----------------------------------
// forward decl
template<class TraitClass1, class TraitClass2, bool TraitFlag> class
MyTestClass;

template<typename T>
struct Trait1SpecializedMember_Helper
{
static void Foo() // dummy funct that has to templ params
{
printf("General\n");
}

template <class MyTestClass > // passing inst of my class so I can call
back into it
static void apply(MyTestClass &that)
{
printf("General - Apply\n");
}
};
// my class
template <class TraitClass1 = int, class TraitClass2=char, bool
TraitFlag=false>
class MyTestClass
{
public:
void DoFuncTrait1()
{
Trait1SpecializedMember_Helper<TraitClass1>::Foo() ; // works
great!
// get errors under Linx saying 'parse error before ';' token

Trait1SpecializedMember_Helper<TraitClass1>::apply <MyTestClass<TraitClass1,T
raitClass2,TraitFlag> >(*this);

// however this works!

Trait1SpecializedMember_Helper<int>::apply<MyTestC lass<TraitClass1,TraitClas
s2,TraitFlag> >(*this);
}
};

------------------------------------

It would appear that I am unable to use template parameters for both the
creation of the struct AND paremeters to a function within that struct.

Meaning I can call: Trait1SpecializedMember_Helper<TraitClass1>::Foo() ;
Just fine and I can call:
Trait1SpecializedMember_Helper<int>::apply<MyTestC lass<TraitClass1,TraitClas
s2,TraitFlag> >(*this); without any problems,

However when I try to call a function that uses template parametes for both
the struct specializaiton and the function call:
Trait1SpecializedMember_Helper<TraitClass1>::apply <MyTestClass<TraitClass1,T
raitClass2,TraitFlag> >(*this);

Some compilers do not like it. Which is find, if I could find a
work-around, for this work-around. :-)

thanx,

Patrick
Jul 22 '05 #6
Patrick Kutch wrote in news:40********@nntp0.pdx.net:
// get errors under Linx saying 'parse error before ';' token

Trait1SpecializedMember_Helper<TraitClass1>::apply <MyTestClass<TraitCla
ss1,T raitClass2,TraitFlag> >(*this);


Note the ::template below:

Trait1SpecializedMember_Helper<TraitClass1>
::template apply<
MyTestClass<TraitClass1,T raitClass2,TraitFlag>(*this)

;

Also since at the above point you're in the declaration of
MyTestClass<> you should be able to write:

Trait1SpecializedMember_Helper<TraitClass1>
::template apply< MyTestClass >(*this)
;

The ::template is required here as the specialization:

Trait1SpecializedMember_Helper< TraitClass1 >

depends on a template paramiter TraitClass1. The compiler needs to be
told that this specialization has a template member apply, otherwise
it assumes that apply is static data and interprets the following <
as the "less than" operator not the opening < of a template paramiter
list. I hope this explains why it worked for Foo() (not a template)
and for <int> (int isn't a dependant name).

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #7
Dude! That was it!

Thanx so much! I would have never been able to figure that out. I thought
I was coming up to speed pretty well with Template, but that kicked my butt.

Do you have any recomendations for some web sites or books so I can better
understand what you showed me?

thanx again!

- Patrick

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.205...
Patrick Kutch wrote in news:40********@nntp0.pdx.net:
// get errors under Linx saying 'parse error before ';' token

Trait1SpecializedMember_Helper<TraitClass1>::apply <MyTestClass<TraitCla
ss1,T raitClass2,TraitFlag> >(*this);


Note the ::template below:

Trait1SpecializedMember_Helper<TraitClass1>
::template apply<
MyTestClass<TraitClass1,T raitClass2,TraitFlag>
>(*this)

;

Also since at the above point you're in the declaration of
MyTestClass<> you should be able to write:

Trait1SpecializedMember_Helper<TraitClass1>
::template apply< MyTestClass >(*this)
;

The ::template is required here as the specialization:

Trait1SpecializedMember_Helper< TraitClass1 >

depends on a template paramiter TraitClass1. The compiler needs to be
told that this specialization has a template member apply, otherwise
it assumes that apply is static data and interprets the following <
as the "less than" operator not the opening < of a template paramiter
list. I hope this explains why it worked for Foo() (not a template)
and for <int> (int isn't a dependant name).

Rob.
--
http://www.victim-prime.dsl.pipex.com/

Jul 22 '05 #8
Patrick Kutch wrote in news:40********@nntp0.pdx.net:

Thanx so much! I would have never been able to figure that out. I
thought I was coming up to speed pretty well with Template, but that
kicked my butt.

Do you have any recomendations for some web sites or books so I can
better understand what you showed me?


You can pick up most of this stuff reading this group,
comp.lang.c++.moderated and comp.std.c++.

Lurking on boost's http://www.boost.org/ developer mailing list
can also be an education:

news://news.gmane.org/gmane.comp.lib.boost.devel.

I haven't got it myself but Andrei Alexandrescu's Modern C++ Design
http://www.moderncppdesign.com. Will probably teach you more than
you really wanted to know, his site contains a whole bunch of links
you might want to follow, including 2 chapters from his book.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #9

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn*********************************@195.129.1 10.201...
Patrick Kutch wrote in news:40********@nntp0.pdx.net:

Thanx so much! I would have never been able to figure that out. I
thought I was coming up to speed pretty well with Template, but that
kicked my butt.

Do you have any recomendations for some web sites or books so I can
better understand what you showed me?


You can pick up most of this stuff reading this group,
comp.lang.c++.moderated and comp.std.c++.

Lurking on boost's http://www.boost.org/ developer mailing list
can also be an education:

news://news.gmane.org/gmane.comp.lib.boost.devel.

I haven't got it myself but Andrei Alexandrescu's Modern C++ Design
http://www.moderncppdesign.com. Will probably teach you more than
you really wanted to know, his site contains a whole bunch of links
you might want to follow, including 2 chapters from his book.

Rob.
--
http://www.victim-prime.dsl.pipex.com/


Thanx.

I am on boot's list. I have Andrei's book - kicked my arse! I loved it.
Still a lot over my head, but I am enjoying the challenge.

Thanx again!
Jul 22 '05 #10

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

Similar topics

8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
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...
1
by: SainTiss | last post by:
Hi, I've been looking into the standard for a clear statement on whether partial specialization of member functions of class templates is allowed or not. 14.7.3/4 says that explicit...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
19
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $...
6
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave...
4
by: Joseph Turian | last post by:
Hi, What is the correct syntax to get the bar<T>::f<int, unsigned>() function to compile in the following fragment? Thanks, Joseph class foo {
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
10
by: jason.cipriani | last post by:
I never seem to be able to get this right. Here I have some code: template <typename T, int Nclass A { void f (T); }; template <typename Tvoid A<T,1>::f (T) { } template <typename Tvoid...
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.