Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

syntactic question w.r.t function passing

21 views
Skip to first unread message

Christof Warlich

unread,
May 22, 2015, 8:39:21 AM5/22/15
to
Hi,

I'm writing a framework where it should be possible for the users of that framework to either pass ordinary C functions or C++ functors to some other function. Basically, this works fine, i.e. the following sample code compiles and works as expected:

#include <iostream>
#include <typeinfo>
template<typename F> struct PassNothing {
PassNothing(F f) {
(*f)();
}
};
struct PrintFunctor {
void operator()() {
std::cout << "Functor" << std::endl;
}
} printFunctor;
void printFunction() {
std::cout << "Function" << std::endl;
}

template<typename F> struct PassThis {
PassThis(F f) {
(*f)(this);
}
};
struct PrintThisFunctor {
void operator()(PassThis<PrintThisFunctor *> *self) {
std::cout << "Functor called from " << typeid(self).name() << std::endl;
}
} printThisFunctor;
void printThisFunction(void *self) {
std::cout << "Function called from " << typeid(self).name() << std::endl;
}

int main() {
PassNothing<PrintFunctor *> passNothingWithFunctor(&printFunctor);
PassNothing<void (*)()> passNothingWithFunction(printFunction);

PassThis<PrintThisFunctor *> passThisWithFunctor(&printThisFunctor);
PassThis<void (*)(void *)> passThisWithFunction(printThisFunction);
return 0;
}

Executing it on my system gives:

$ ./tst
Functor
Function
Functor called from P8PassThisIP16PrintThisFunctorE
Function called from Pv

But the last line of the output shows my issue: As the code is currently written, the type information of the calling "PassThis" class gets lost due to the upcast to "void *" done in "printThisFunction". But while I could get the proper type being passed one line above for "printThisFunctor", I'm unable to do the same for "printThisFunction".

Any ideas how this could be achieved, if possible with plain C++ 2003?

Victor Bazarov

unread,
May 22, 2015, 9:21:26 AM5/22/15
to
I am not sure I understand what you mean. What "type information" did
you expect not to get "lost"? IOW, what do you *want* to have?

> But
while I could get the proper type being passed one line above for
"printThisFunctor", I'm unable to do the same for "printThisFunction".

You're unable because it causes infinite recursion.

> Any ideas how this could be achieved, if possible with plain C++ 2003?

How could *what* be achieved? You've hit the known wall of
impossibility to declare a function that has as an argument a pointer to
the same function (in your case wrapped in a template, which is the same
thing). There is no solution for it.

What is it you're trying to accomplish? Why do you think you need to
pass the pointer to "self" into that function? Are you implementing
some kind of reflection?

V
--
I do not respond to top-posted replies, please don't ask

Christof Warlich

unread,
May 22, 2015, 10:27:14 AM5/22/15
to
> I am not sure I understand what you mean. What "type information" did
> you expect not to get "lost"? IOW, what do you *want* to have?

I'd like to see the same behaviour for "printThisFunction" as I already got for "PrintThisFunctor": Passing the calling object's "this" pointer to operator()() without loosing its type information.

> > But while I could get the proper type being passed one line above for
> > "printThisFunctor", I'm unable to do the same for "printThisFunction".
>
> You're unable because it causes infinite recursion.

Yes, this is what is look like.

> > Any ideas how this could be achieved, if possible with plain C++ 2003?
>
> How could *what* be achieved? You've hit the known wall of
> impossibility to declare a function that has as an argument a pointer to
> the same function (in your case wrapped in a template, which is the same
> thing). There is no solution for it.

Ok, thanks for the clear statement: I just wanted to ensure that I did not
miss some obvious fix.

> What is it you're trying to accomplish? Why do you think you need to
> pass the pointer to "self" into that function? Are you implementing
> some kind of reflection?

No, it's some kind of "threading" framework, and passing "this" just allows to get hold of one's own "thread".

Victor Bazarov

unread,
May 22, 2015, 11:04:55 AM5/22/15
to
On 5/22/2015 10:27 AM, Christof Warlich wrote:
> Victor Bazarov wrote:
(attribution was missing)
>> What is it you're trying to accomplish? Why do you think you need to
>> pass the pointer to "self" into that function? Are you implementing
>> some kind of reflection?
>
> No, it's some kind of "threading" framework, and passing "this" just
> allows to get hold of one's own "thread".

I can't claim of deep understanding what a "threading framework" would
imply in such a case, forgive my ignorance. I always thought that
functions and functors ought to be written oblivious to threads in which
they are called. But you're the boss...

Perhaps if you showed how threads are figuring into your stuff, and for
what you needed the "this" in your code, folks who deal with threads
more would be able to advise you with higher precision.
0 new messages