473,385 Members | 1,337 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,385 software developers and data experts.

pointer to member function as a template parameter

ank
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! ]

Oct 30 '06 #1
4 3543
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:ja*********@gmail.com
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! ]

Oct 31 '06 #2
ank
{ 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! ]

Nov 1 '06 #3
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! ]

Nov 1 '06 #4
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: ja*********@gmail.com
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! ]

Nov 1 '06 #5

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

Similar topics

3
by: jimjim | last post by:
Hello, My question concerns as to how a pointer is passed by reference as a function argument. The following is from code taken from the MICO implementation of the CORBA specification. in...
3
by: LinusLee | last post by:
class CAnalyzer { // attribute private: public: // behavior private: void NullFunc(NODE *pNode); void TraverseNode(NODE* pNode, void (*preFunc)(NODE *node), void (*postFunc)(NODE *node));
15
by: Albert | last post by:
Hi, I need to pass a pointer-to-member-function as a parameter to a function which takes pointer-to-function as an argument. Is there any way to do it besides overloading the function? Here...
4
by: firegun9 | last post by:
Hello everyone, here is my program: /////////////////////////////// #include <iostream> using namespace std; void multi(double* arrayPtr, int len){ for(int i=0; i<len; i++)...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
12
by: WaterWalk | last post by:
Hello. I am rather confused by the type of a pointer to class data member. Many c++ texts say that pointer to data member has a special syntax. For example the following class: class MyClass {...
3
by: George2 | last post by:
Hello everyone, In the following statements, template <class R, class Tclass mem_fun_t : public unary_function <T*, R> {
5
by: jknupp | last post by:
In the following program, if the call to bar does not specify the type as <int>, gcc gives the error "no matching function for call to ‘bar(A&, <unresolved overloaded function type>)’". Since bar...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.