hi, expert. i find that we can define a function pointer for template
argument.
for exmaple:

template<typename void(*err_handle)()> class List{/*...*/};
...

void err_handler1()
{};
...
List<int, &err_hander1> x1; // OK........


now, my question is we can define a pointer that point to non-static member
function? i have tested 2 cases:

Case 1.
template<typename T, void (T::*pfuncType)(const T& ) > class a_t {};

class b
{
public:
void non_virtual(const b& ) {};
virtual void test_virtual(const b& ) {};
protected:
private:
};
a_t<b,&b::non_virtual> g_non_virtaul;
a_t<b,&b::test_virtual> g_test_virtaul;

compile it successfully!

Case 2.
template<typename T, void (T::*pfuncType)(const T& ) > class a_t {};
class b
{
public:
void non_virtual(const b& ) {};
virtual void test_virtual(const b& ) {};

a_t<b,&b::non_virtual> m_non_virtaul;
a_t<b,&b::test_virtual> m_test_virtaul;

protected:
private:
};

I can not compile it, why?
the error message is error C2327: 'b::non_virtual' : member from enclosing
class is not a type name, static, or enumerator

Re: why it is valid? by tom_usenet

tom_usenet
Wed Dec 17 04:20:57 CST 2003

On Wed, 17 Dec 2003 16:34:14 +0800, "chenchang" <baibaichen@sohu.com>
wrote:
>now, my question is we can define a pointer that point to non-static member
>function?

Yes.

>i have tested 2 cases:
>
>Case 1.
>template<typename T, void (T::*pfuncType)(const T& ) > class a_t {};
>
>class b
>{
>public:
> void non_virtual(const b& ) {};
> virtual void test_virtual(const b& ) {};
>protected:
>private:
>};
>a_t<b,&b::non_virtual> g_non_virtaul;
>a_t<b,&b::test_virtual> g_test_virtaul;
>
>compile it successfully!

Right, it looks fine.

>
>Case 2.
>template<typename T, void (T::*pfuncType)(const T& ) > class a_t {};
>class b
>{
>public:
> void non_virtual(const b& ) {};
> virtual void test_virtual(const b& ) {};
>
> a_t<b,&b::non_virtual> m_non_virtaul;
> a_t<b,&b::test_virtual> m_test_virtaul;
>
>protected:
>private:
>};
>
>I can not compile it, why?

The code is fine - I suspect your compiler is out of date.

>the error message is error C2327: 'b::non_virtual' : member from enclosing
>class is not a type name, static, or enumerator

VC 7.1 compiles it without error.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Re: why it is valid? by chenchang

chenchang
Wed Dec 17 18:56:20 CST 2003

hi, thanks ur reply. but i can not use vc 7.1, some of our code can not
compiled by vc7.1. and it costs more time to port them to vc7.1.

so, do u have any idea to emulate under vc7?

thanks..

"tom_usenet" <tom_usenet@hotmail.com> wrote in message
news:pab0uv461p8ok5a94t05shgm1si84caq6m@4ax.com...
> On Wed, 17 Dec 2003 16:34:14 +0800, "chenchang" <baibaichen@sohu.com>
> wrote:
> >now, my question is we can define a pointer that point to non-static
member
> >function?
>
> Yes.
>
> >i have tested 2 cases:
> >
> >Case 1.
> >template<typename T, void (T::*pfuncType)(const T& ) > class a_t {};
> >
> >class b
> >{
> >public:
> > void non_virtual(const b& ) {};
> > virtual void test_virtual(const b& ) {};
> >protected:
> >private:
> >};
> >a_t<b,&b::non_virtual> g_non_virtaul;
> >a_t<b,&b::test_virtual> g_test_virtaul;
> >
> >compile it successfully!
>
> Right, it looks fine.
>
> >
> >Case 2.
> >template<typename T, void (T::*pfuncType)(const T& ) > class a_t {};
> >class b
> >{
> >public:
> > void non_virtual(const b& ) {};
> > virtual void test_virtual(const b& ) {};
> >
> > a_t<b,&b::non_virtual> m_non_virtaul;
> > a_t<b,&b::test_virtual> m_test_virtaul;
> >
> >protected:
> >private:
> >};
> >
> >I can not compile it, why?
>
> The code is fine - I suspect your compiler is out of date.
>
> >the error message is error C2327: 'b::non_virtual' : member from
enclosing
> >class is not a type name, static, or enumerator
>
> VC 7.1 compiles it without error.
>
> Tom
>
> C++ FAQ: http://www.parashift.com/c++-faq-lite/
> C FAQ: http://www.eskimo.com/~scs/C-faq/top.html