C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Specialzation of Template Member Functions.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
DblQt
Guest





PostPosted: Thu Jan 29, 2004 3:09 pm    Post subject: Specialzation of Template Member Functions. Reply with quote



Hi,

I wonder whether it is possible to specialize certain member functions
instead of whole templates.

Example:

<code>
template<class T, bool b> Example {
public:
T* m_instance;
// Implementation should be dependent on b.
void DoSomething();
};

template<class T, bool b> void Example<T, true>::DoSomething() {
// Execute special code that has to be provided by T

// Delegate must be defined in T
m_instance->Delegate();
}

template<class T, bool b> void Example<T, false>::DoSomething() {
// Execute code that is not special to T.
}
</code>

This is similar to what I've tried to do. Of course this is not a
complete definition and can't be a correct implementation either since
it doesn't compile. I'm wondering whether something like this is
possible at all.

Kind Regrads
Stefan

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
xueweizhong
Guest





PostPosted: Fri Jan 30, 2004 9:35 am    Post subject: Re: Specialzation of Template Member Functions. Reply with quote



template <class T, bool b>
struct Example
{
void DoSomething();
};
------------------------------------------#t1

template<class T>
void Example<T, true>::DoSomething();
------------------------------------------#1

template<class T>
void Example<T, false>::DoSomething();
------------------------------------------#2

in the context of #t1
both #1 && #2 are partial specialization of #3:

template <class T, bool b>
void Example<T, b>::DoSomething();
------------------------------------------#3

because #3 is a member function template,
and BY C++98
functon template can't be partial specialized,
^^^^^^^
so that #1 && #2 is illegal.

Note that the following program is OK:

template <class T, bool b>
struct Example
{
void DoSomething();
};

template <>
void Example<bool, true>::DoSomething()
{
}

int main()
{
Example<bool, true> a;
a.DoSomething();
}

BUT if you provides the specialization of template class:

template <class T, bool b>
struct Example<T, false> { /*...*/ };
------------------------------------------#t2

template <class T>
struct Example<T, true> { /*....*/ };
------------------------------------------#t3

then, #1 becomes the member function template of #t2
and #2 becomes the member function template of #t3

now, #1, #2 is not partial specialization of #3,
they are different class-template (#t2#t3)'s member function.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Dhruv
Guest





PostPosted: Fri Jan 30, 2004 9:37 am    Post subject: Re: Specialzation of Template Member Functions. Reply with quote



On Thu, 29 Jan 2004 10:09:16 -0500, DblQt wrote:

Quote:
Hi,

I wonder whether it is possible to specialize certain member functions
instead of whole templates.

No. partial specialization of functions is not permitted.


Regards,
-Dhruv.




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Martijn Lievaart
Guest





PostPosted: Thu Feb 05, 2004 11:38 am    Post subject: Re: Specialzation of Template Member Functions. Reply with quote

On Thu, 29 Jan 2004 10:09:16 -0500, DblQt wrote:

Quote:
Hi,

I wonder whether it is possible to specialize certain member functions
instead of whole templates.

Example:

code
template<class T, bool b> Example {
public:
T* m_instance;
// Implementation should be dependent on b.
void DoSomething();
};

template<class T, bool b> void Example<T, true>::DoSomething() {
// Execute special code that has to be provided by T

// Delegate must be defined in T
m_instance->Delegate();
}

template<class T, bool b> void Example<T, false>::DoSomething() {
// Execute code that is not special to T.
}
/code

This is similar to what I've tried to do. Of course this is not a
complete definition and can't be a correct implementation either since
it doesn't compile. I'm wondering whether something like this is
possible at all.

// Not sure if this compiles, but the basic idea is there

template<class T, bool b> Example {
template<bool b> struct X;
template<> struct X<true> {
X(T *p) { p->delegate(); }
};
template<> struct X<false> {
X(T *p) {}
};

public:
T* m_instance;
// Implementation should be dependent on b.
void DoSomething() { X<b>(); }
};

HTH,
M4


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Martijn Lievaart
Guest





PostPosted: Fri Feb 06, 2004 12:33 pm    Post subject: Re: Specialzation of Template Member Functions. Reply with quote

On Thu, 05 Feb 2004 06:38:28 -0500, Martijn Lievaart wrote:

Quote:
public:
T* m_instance;
// Implementation should be dependent on b.
void DoSomething() { X<b>(); }

Oopsa:

void DoSomething() { X<b>(m_instance); }

M4


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.