On 14 June 2012 22:42, Oleg Endo wrote:
> On Thu, 2012-06-14 at 16:34 -0400, Rick C. Hodgin wrote:
>> David,
>>
>> Well, I probably don't have a NEED for it. I've gotten along for 25+
>> years without it. :-)
>>
>> However, what prompted my inquiry is using it would've saved me tracking
>> down a few bugs in recent weeks. Some prior code was re-used for a
>> similar function, but the name of the recursive calls weren't updated in
>> every case. It didn't take long to debug, but I realized that had it
>> always been written as self() it never would've been an issue.
>>
>> I can also see a use for generated code where there's a base source code
>> template in use with an embedded include file reference that changes as
>> it's generated per pass, such as:
>>
>> int step1(int a, int b)
>> {
>> #include "\current_task\step1.cpp"
>> }
>>
>> int step2(int a, int b)
>> {
>> #include "\current_task\step2.cpp"
>> }
>>
>> Using the self() reference for recursion, one could modify stepN.cpp's
>> generator algorithms without having to know or care anything in the
>> wrapper code.
>
> Wouldn't this do?
>
> #define __self__ step1
> int __self__ (int a, int b)
> {
> #include "something"
> __self__ (x, y);
> }
> #undef __self__
You can already do this with GCC in C and C++ (minus problems with
overloaded functions) like this:
#define DECLSELF(f,self) __typeof__ (&f) self = f
int foo (int n)
{
DECLSELF(foo, self);
if (n == 0)
return 0;
else
{
return 1 + self (n - 1);
}
}
--
VZ