(member) function returning pointer to functions like itself?
Author Message
(member) function returning pointer to functions like itself?

Consider:

...
T f1 ();
T f2 ();
...
T fN ();
...
T f1 () {
...
return f2;

Quote:
}

...
T fN () {
...
return f1;
Quote:
}

...
T t;
...
t = (*t) ();    // I really want it like this
...

How do I define T?

Cheers!

Dima

Sun, 15 Sep 2002 03:00:00 GMT
(member) function returning pointer to functions like itself?

Quote:

> Consider:

> ...
> T f1 ();
> T f2 ();
> ...
> T fN ();
> ...
> T f1 () {
> ...
> return f2;
> }
> ...
> T fN () {
> ...
> return f1;
> }
> ...
> T t;
> ...
> t = (*t) (); // I really want it like this

You cannot do that.  You're calling a function using
undefined pointer.  Declaration T t; leaves 't' uninitialised.

Quote:
> ...

> How do I define T?

I strongly doubt it is possible.  It's infinitely recursive.

T is
a pointer to a function that takes no parameters and returns a pointer to
a function that takes no parameters and returns a pointer to a function
that takes no parameters and returns a pointer to a function that takes no
parameters and returns a pointer to a function that takes no parameters
and returns a pointer to a function that takes no parameters and returns
a pointer to a function that takes no parameters and returns a pointer to
a function that takes no parameters and returns a pointer to a function
that takes no parameters and returns a pointer to a function that takes no
parameters and returns a pointer to a function that takes no parameters
and returns a pointer to a function that takes no parameters and returns
a pointer to a function that takes no parameters and returns a pointer to
a function that takes no parameters and returns ...

I would like to see others prove me wrong.

Victor
--
Please remove capital A's from my address when replying by mail

Sun, 15 Sep 2002 03:00:00 GMT
(member) function returning pointer to functions like itself?

Quote:

>Consider:

>...
>T f1 ();
>T f2 ();
>...
>T fN ();
>...
>T f1 () {
>    ...
>    return f2;
>}
>...
>T fN () {
>    ...
>    return f1;
>}
>...
>T t;
>...
>    t = (*t) ();    // I really want it like this

You can't do this directly; one way is to use a struct wrapper:

struct self_ptr {
struct self_ptr (*function_ptr)(void);
};

struct self_ptr function(void)
{
struct self_ptr return_value;
return_value.function_ptr = function;
return return_value;
}

As for C++ member functions, that is off topic in comp.lang.c.
Static member functions can be handled in a similar way.
Non-statics will differ in that they will return a pointer to member:

class C;

struct self_ptr {
struct self_ptr (C::* function_ptr)(); // pointer to member
};

class C {
public:
struct self_ptr memb_func();
// ...
};

struct self_ptr C::memb_func()
{
struct self_ptr return_value;
return_value.function_ptr = &C::memb_func;
return return_value;
}

--
#exclude <windows.h>

Sun, 15 Sep 2002 03:00:00 GMT
(member) function returning pointer to functions like itself?

Quote:

> > T t;
> > ...
> > t = (*t) (); // I really want it like this

> You cannot do that.  You're calling a function using
> undefined pointer.  Declaration T t; leaves 't' uninitialised.

I didn't put ellipsis there for nothing.

Quote:
> I strongly doubt it is possible.  It's infinitely recursive.

It would've been possible if I could write

typedef T* (*T) ();

but nor C neither C++ allow me do that. On the other hand, I can use
things like

class T {
...
class T f();
// or
class T* g ();
...

Quote:
};

It's good enough for a workaround, but it doesn't give me the same
performance (at least with g++) as I would've got if I were using
pointers to functions. Besides, it's not C.

Quote:
> Victor

Dima

Sun, 15 Sep 2002 03:00:00 GMT
(member) function returning pointer to functions like itself?

Quote:

> >       t = (*t) ();    // I really want it like this

> You can't do this directly; one way is to use a struct wrapper:

>     struct self_ptr {
>         struct self_ptr (*function_ptr)(void);
>     };

>     struct self_ptr function(void)
>     {
>         struct self_ptr return_value;
>         return_value.function_ptr = function;
>         return return_value;
>     }

Yep, that's what I'd ended up with. As I wrote already, the problem is
that (at least in Solaris) gcc/g++ doesn't pass struct results on
registers and it really slows the whole thing down.

Quote:
> As for C++ member functions, that is off topic in comp.lang.c.

You've cross-posted to comp.lang.c++ :-)

Quote:
> Static member functions can be handled in a similar way.

Cheers!

Dima

Sun, 15 Sep 2002 03:00:00 GMT
(member) function returning pointer to functions like itself?

Quote:

>> >       t = (*t) ();    // I really want it like this

>> You can't do this directly; one way is to use a struct wrapper:

>>     struct self_ptr {
>>         struct self_ptr (*function_ptr)(void);
>>     };

>>     struct self_ptr function(void)
>>     {
>>         struct self_ptr return_value;
>>         return_value.function_ptr = function;
>>         return return_value;
>>     }

>Yep, that's what I'd ended up with. As I wrote already, the problem is
>that (at least in Solaris) gcc/g++ doesn't pass struct results on
>registers and it really slows the whole thing down.

If you don't like that trick, another one you can use is simply to
cast the function's address to a generic function pointer:

typedef void (*generic_fptr_t)(void);

generic_fptr_t function(void)
{
return (generic_fptr_t) function;
}

Then you can cast this back to the original type before making the call.
ANSI C sanctions this use: one pointer-to-function type can be converted
to another and the back to the original.

Sun, 15 Sep 2002 03:00:00 GMT
(member) function returning pointer to functions like itself?

Quote:

> Consider:

> ...
> T f1 ();
> T f2 ();
> ...
> T fN ();
> ...
> T f1 () {
>         ...
>         return f2;
> }
> ...
> T fN () {
>         ...
>         return f1;
> }
> ...
> T t;
> ...
>         t = (*t) ();    // I really want it like this
> ...

> How do I define T?

> Cheers!

> Dima

I am curious as to what the purpose of this exercise is.
The following is not quite the same as what you want,
but it is the closest I can get to and also looks trivial.
Maybe I have misunderstood your post. In that case,
please just ignore this nonsense :-)
-------------
#include <iostream>
using namespace std;
void* f3();
void* f1() {
cout<<"f1"<<endl;
return f3;
Quote:
}

void* f2() {
cout<<"f2"<<endl;
return f1;
Quote:
}

void* f3() {
cout<<"f3"<<endl;
return f2;
Quote:
}

int main() {
void *i1=f1();
void *i2=f2();
void *i3=f3();
cout<<i1<<" "<<i2<<" "<<i3<<endl;
return 0;

Quote:
}

--
*  Fei Li,                | (757)-865-0818                   *

*  28 Research Drive      | http://www.htc-tech.com/home/fli *
*  Hampton, VA23666       |                                  *

Sun, 15 Sep 2002 03:00:00 GMT
(member) function returning pointer to functions like itself?

Quote:

> > > T t;
> > > ...
> > > t = (*t) (); // I really want it like this

> > You cannot do that.  You're calling a function using
> > undefined pointer.  Declaration T t; leaves 't' uninitialised.

> I didn't put ellipsis there for nothing.

> > I strongly doubt it is possible.  It's infinitely recursive.

> It would've been possible if I could write

> typedef T* (*T) ();

That would be a pointer to a pointer to a function, wouldn't it?

Quote:

> but nor C neither C++ allow me do that. On the other hand, I can use
> things like

> class T {
> ...
> class T f();
> // or
> class T* g ();
> ...
> };

In C++ (and forgive me for still cross-posting in comp.lang.c)
you can do this:
===================================================
#include <iostream>

using std::cout;
using std::endl;

class T
{
T (*function)();
public:
T() : function(0) {}
T(T (*a)()) : function(a) {}

const T& operator=(const T& t) { function = t.function; return *this; }
T operator()() { if (function) return function(); else return *this; }

///// this is just for running this example
friend std::ostream& operator <<(std::ostream& s, const T&);

Quote:
};

///// this is just for running this example
std::ostream& operator << (std::ostream& s, const T& t)
{
return s << t.function;

Quote:
}

T t1();
T t2();

T t1()
{
return t2;

Quote:
}

T t2()
{
return t1;

Quote:
}

int main()
{
T t = t1;
std::cout << t << std::endl;
t = t();
std::cout << t << std::endl;
t = t();
std::cout << t << std::endl;

return 0;

Quote:
}

===================================================

Quote:

> It's good enough for a workaround, but it doesn't give me the same
> performance (at least with g++) as I would've got if I were using
> pointers to functions. Besides, it's not C.

> > Victor

> Dima

Victor
--
Please remove capital A's from my address when replying by mail

Sun, 15 Sep 2002 03:00:00 GMT
(member) function returning pointer to functions like itself?

Quote:

> > It would've been possible if I could write

> > typedef T* (*T) ();

> That would be a pointer to a pointer to a function, wouldn't it?

Yep, to pick a nit drop the first asterisk. It won't help with compiling
such a "construct" though.

Quote:
> In C++ (and forgive me for still cross-posting in comp.lang.c)
> you can do this:
> ===================================================
> #include <iostream>

> using std::cout;
> using std::endl;

> class T
> {
>     T (*function)();
> public:
>     T() : function(0) {}
>     T(T (*a)()) : function(a) {}

>     const T& operator=(const T& t) { function = t.function; return *this; }
>     T operator()() { if (function) return function(); else return *this; }

>     ///// this is just for running this example
>     friend std::ostream& operator <<(std::ostream& s, const T&);
> };

> ///// this is just for running this example
> std::ostream& operator << (std::ostream& s, const T& t)
> {
>     return s << t.function;
> }

> T t1();
> T t2();

> T t1()
> {
>     return t2;
> }

> T t2()
> {
>     return t1;
> }

> int main()
> {
>     T t = t1;
>     std::cout << t << std::endl;
>     t = t();
>     std::cout << t << std::endl;
>     t = t();
>     std::cout << t << std::endl;

>     return 0;
> }
> ===================================================

Yep, this is exactly what I had to resort to (the only real difference
is I don't see any reason for your explicit assignment operator), and
what Kaz proposed for C.

Quote:
> Victor

Dima

Sun, 15 Sep 2002 03:00:00 GMT
(member) function returning pointer to functions like itself?

Quote:

> If you don't like that trick, another one you can use is simply to
> cast the function's address to a generic function pointer:

>     typedef void (*generic_fptr_t)(void);

>     generic_fptr_t function(void)
>     {
>         return (generic_fptr_t) function;
>     }

> Then you can cast this back to the original type before making the call.
> ANSI C sanctions this use: one pointer-to-function type can be converted
> to another and the back to the original.

Now this is a good one. Thanks a bunch, Kaz!

Dima

Sun, 15 Sep 2002 03:00:00 GMT
(member) function returning pointer to functions like itself?

Quote:

> I am curious as to what the purpose of this exercise is.

It's not exactly an exercise - it's an attempt to speed up a certain
program :-)

Quote:
> The following is not quite the same as what you want,
> but it is the closest I can get to and also looks trivial.
> Maybe I have misunderstood your post. In that case,
> please just ignore this nonsense :-)
> -------------
> #include <iostream>
> using namespace std;
> void* f3();
> void* f1() {
>     cout<<"f1"<<endl;
>     return f3;
> }
> void* f2() {
>     cout<<"f2"<<endl;
>     return f1;
> }
> void* f3() {
>     cout<<"f3"<<endl;
>     return f2;
> }
> int main() {
>    void *i1=f1();
>    void *i2=f2();
>    void *i3=f3();
>    cout<<i1<<" "<<i2<<" "<<i3<<endl;
>    return 0;
> }

Now the trick is to invoke a function that i1 or i2 or i3 points to.

Quote:
>   *  Fei Li,                | (757)-865-0818                   *

Cheers!

Dima

Sun, 15 Sep 2002 03:00:00 GMT
(member) function returning pointer to functions like itself?

Quote:

> > If you don't like that trick, another one you can use is simply to
> > cast the function's address to a generic function pointer:

> >     typedef void (*generic_fptr_t)(void);

> >     generic_fptr_t function(void)
> >     {
> >         return (generic_fptr_t) function;
> >     }

> > Then you can cast this back to the original type before making the call.
> > ANSI C sanctions this use: one pointer-to-function type can be converted
> > to another and the back to the original.

> Now this is a good one. Thanks a bunch, Kaz!

Now if you could also help me with making the same trick with member
functions...

Quote:
> Dima

Cheers!

Dima

Sun, 15 Sep 2002 03:00:00 GMT
(member) function returning pointer to functions like itself?

Quote:

> Now the trick is to invoke a function that i1 or i2 or i3 points to.

> Cheers!

> Dima

Using casts is the only way I can think of.
------------------
#include <iostream>
using namespace std;
void * f3();
void* f1() {
cout<<"f1"<<endl;
return f3;
Quote:
}

void* f2() {
cout<<"f2"<<endl;
return f1;
Quote:
}

void* f3() {
cout<<"f3"<<endl;
return f2;
Quote:
}

int main() {
void *(*i1)() =static_cast<void* (*)()>(f1());
void *(*i2)() =static_cast<void* (*)()>(f2());
void *(*i3)() =static_cast<void* (*)()>(f3());

i1();
i2();
i3();
return 0;

Quote:
}

--
*  Fei Li,                | (757)-865-0818                   *

*  28 Research Drive      | http://www.htc-tech.com/home/fli *
*  Hampton, VA23666       |                                  *

Sun, 15 Sep 2002 03:00:00 GMT
(member) function returning pointer to functions like itself?

Quote:

> #include <iostream>

Please don't post C++ in comp.lang.c.
Followups set.

Thanks,

Ben.

Sun, 15 Sep 2002 03:00:00 GMT
(member) function returning pointer to functions like itself?

Quote:

>> > It would've been possible if I could write

>> > typedef T* (*T) ();

>> That would be a pointer to a pointer to a function, wouldn't it?
> Yep, to pick a nit drop the first asterisk. It won't help with compiling
> such a "construct" though.

it is not.  it's a pointer to a function returning a pointer.

--
/"\                              m i k e    b u r r e l l

X        AGAINST HTML MAIL      http://mikpos.dyndns.org
/ \

Sun, 15 Sep 2002 03:00:00 GMT

 Page 1 of 2 [ 25 post ] Go to page: [1] [2]

Relevant Pages