Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pointer to member function as a template parameter

Reply
Thread Tools

pointer to member function as a template parameter

 
 
ank
Guest
Posts: n/a
 
      10-30-2006
Hi,

I write simple class to transform member function into non member
function
(with some rearrangement of its parameter).

The code is:

template<class T_, int (T_::*mfn_)()>
class Mfn {
public:
static int fn(T_* pT) { return (pT->*mfn_)(); }
};

and then try it with some test class like this:

class Test {
private:
// note that this member function is private
int test()
{
std::cout << "Test::test()" << std::endl;
return 0;
}
};

Then Mfn is used like this:

Test t;
Mfn<Test, &Test::test>::fn(&t);

The question is: "What is the sensible behavior of this code?"
Possible answer is:
1. Fail to compile outside of class "Test" (if it is used inside Test,
it should be OK)
2. Undefined behavior

I have tested it and the code compiles cleanly for 1 compiler (I don't
know about other compiler)
but if I change the member function to static and change the template
parameter accordingly,
it obviously failed as I expected.

I expected the test code to fail outside class "Test"

I don't really know if the standard defined this case or not.

Regards,
Ekaphol


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

 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      10-31-2006
ank wrote:

> I write simple class to transform member function into non
> member function (with some rearrangement of its parameter).


> The code is:


> template<class T_, int (T_::*mfn_)()>
> class Mfn {
> public:
> static int fn(T_* pT) { return (pT->*mfn_)(); }
> };


> and then try it with some test class like this:


> class Test {
> private:
> // note that this member function is private
> int test()
> {
> std::cout << "Test::test()" << std::endl;
> return 0;
> }
> };


> Then Mfn is used like this:


> Test t;
> Mfn<Test, &Test::test>::fn(&t);


> The question is: "What is the sensible behavior of this code?"
> Possible answer is:
> 1. Fail to compile outside of class "Test" (if it is used
> inside Test, it should be OK)


I don't see where there can be any question about it. What is
private is the declaration. Outside of the class, any attempt
to use the declaration is illegal. How you are attempting to
use it is irrelevant.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


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

 
Reply With Quote
 
 
 
 
ank
Guest
Posts: n/a
 
      11-01-2006
{ Please do not quote the signature and the banner. -mod }

James Kanze wrote:
>
> I don't see where there can be any question about it. What is
> private is the declaration. Outside of the class, any attempt
> to use the declaration is illegal. How you are attempting to
> use it is irrelevant.


Are you saying that according to the standard,
this code should not compile (of course, if use outside class Test),
right?

What if I use the code inside the class like this

class Test {
public:
int trySomething()
{
Test t;
Mfn<Test, &Test::test>::fn(&t);
}
private:
// note that this member function is private
int test()
{
std::cout << "Test::test()" << std::endl;
return 0;
}

};

&Test::test is accessible inside Test

Is this program well-formed?

I just want to know if the compiler should instantiate the template
with its parameter as a name that has been private in some class or not.


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

 
Reply With Quote
 
L.Suresh
Guest
Posts: n/a
 
      11-01-2006
> Is this program well-formed?
Yes.

> I just want to know if the compiler should instantiate the template
> with its parameter as a name that has been private in some class or not.


it depends on where it is used. here, it has the accessibility to take
the address.


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

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-01-2006
ank wrote:
> James Kanze wrote:


> > I don't see where there can be any question about it. What is
> > private is the declaration. Outside of the class, any attempt
> > to use the declaration is illegal. How you are attempting to
> > use it is irrelevant.


> Are you saying that according to the standard, this code
> should not compile (of course, if use outside class Test),
> right?


Maybe. It's an error which requires a diagnostic. A compiler
could issue a diagnostic, and then compile it anyway. Normally,
as a quality of implementation issue, I would be against this,
but if the compiler had allowed such code in its pre-standard
versions, it is quite reasonable for it to continue allowing it,
rather than breaking user code; in such cases, several compilers
emit a diagnostic along the lines of "anacronism", and still
compile the code.

> What if I use the code inside the class like this


> class Test {
> public:
> int trySomething()
> {
> Test t;
> Mfn<Test, &Test::test>::fn(&t);
> }
> private:
> // note that this member function is private
> int test()
> {
> std::cout << "Test::test()" << std::endl;
> return 0;
> }
>
> };


> &Test::test is accessible inside Test


> Is this program well-formed?


Sure.

> I just want to know if the compiler should instantiate the
> template with its parameter as a name that has been private in
> some class or not.


The instantiation doesn't really use the name directly, and
access is never checked on the arguments of a template. You can
only trigger the instantiation of a class, however, in a context
where you can name the type. (It is possible, however, to
instantiate a function with a private type even outside of the
class, although I doubt that such cases occur very often in real
code.)

--
James Kanze (Gabi Software) email: http://www.velocityreviews.com/forums/(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing a member function as a parameter of a member function Azdo C++ 2 03-06-2007 11:21 AM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments