 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
DblQt Guest
|
Posted: Thu Jan 29, 2004 3:09 pm Post subject: Specialzation of Template Member Functions. |
|
|
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
|
Posted: Fri Jan 30, 2004 9:35 am Post subject: Re: Specialzation of Template Member Functions. |
|
|
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
|
Posted: Fri Jan 30, 2004 9:37 am Post subject: Re: Specialzation of Template Member Functions. |
|
|
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
|
Posted: Thu Feb 05, 2004 11:38 am Post subject: Re: Specialzation of Template Member Functions. |
|
|
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
|
Posted: Fri Feb 06, 2004 12:33 pm Post subject: Re: Specialzation of Template Member Functions. |
|
|
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 |
|
 |
|
|
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
|
|