Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How can you declare function f that...

Reply
Thread Tools

How can you declare function f that...

 
 
benben
Guest
Posts: n/a
 
      12-15-2006
How do you declare a function f that takes a parameter that is a pointer
to itself?

So you can do f(f);

Pardon my curiosity!

Ben
 
Reply With Quote
 
 
 
 
Vyacheslav Kononenko
Guest
Posts: n/a
 
      12-15-2006


On Dec 14, 10:57 pm, benben <benhonghatgmaildotcom@nospam> wrote:
> How do you declare a function f that takes a parameter that is a pointer
> to itself?
>
> So you can do f(f);
>
> Pardon my curiosity!
>
> Ben


struct Boo { template<class T>Boo( T ); };

void f( Boo );

void foo()
{
f( f );
}

 
Reply With Quote
 
 
 
 
softcoder
Guest
Posts: n/a
 
      12-15-2006
Pardon my curiosity! but can anybody please explain why this code
works?

-softcoder


Vyacheslav Kononenko wrote:
> On Dec 14, 10:57 pm, benben <benhonghatgmaildotcom@nospam> wrote:
> > How do you declare a function f that takes a parameter that is a pointer
> > to itself?
> >
> > So you can do f(f);
> >
> > Pardon my curiosity!
> >
> > Ben

>
> struct Boo { template<class T>Boo( T ); };
>
> void f( Boo );
>
> void foo()
> {
> f( f );
> }


 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      12-15-2006
"softcoder" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com
>
> Vyacheslav Kononenko wrote:
>> On Dec 14, 10:57 pm, benben <benhonghatgmaildotcom@nospam> wrote:
>>> How do you declare a function f that takes a parameter that is a
>>> pointer to itself?
>>>
>>> So you can do f(f);
>>>
>>> Pardon my curiosity!
>>>
>>> Ben

>>
>> struct Boo { template<class T>Boo( T ); };
>>
>> void f( Boo );
>>
>> void foo()
>> {
>> f( f );
>> }

>
> Pardon my curiosity! but can anybody please explain why this code
> works?
>
> -softcoder



Does it? At least on my compiler, it fails to link. This is easily corrected
with a couple of empty definitions, but it still doesn't give the results
one might want.

struct Boo { template<class T>Boo( T ); };

which I would re-write as:

struct Boo
{
template<class T>
Boo( T );
};

gives Boo a templated constructor. You can pass Boo's constructor an
argument of any type and a constructor that takes that type will be produced
by the template mechanism. Thus, when you pass it f, the template parameter
T becomes f.

void f( Boo );

defines f to accept a Boo parameter. Thus when

f(f);

is called, f is converted to a Boo temporary using Boo's templated
constructor and this Boo temporary is passed to f (or at least it would be
if the compiler didn't optimise it away).

Accordingly, while f is used as the function argument, the f function never
gets to see this argument, which rather defeats the purpose (at least you
would think so; the OP didn't explain the purpose beyond satisfying his
curiousity).

Observe that recursive functions call themselves without needing a function
pointer to themselves, so it is not clear what the point of

f(f);

might be.

With function objects, you can achieve something that "works" after a
fashion:

struct F
{
F() : count(0)
{}
void operator()(F& f)
{
cout << "f\n";
++count;
if(count < 10)
f(*this);
}
int count;
};

int main()
{
F f;
f(f);

return 0;
}

--
John Carson


 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      12-15-2006
On Fri, 15 Dec 2006 14:57:51 +1100, benben wrote:

> How do you declare a function f that takes a parameter that is a pointer
> to itself?
>
> So you can do f(f);


struct ftype
{
void operator() (ftype) const {}
};

int main() {
ftype f;
f(f);
}

Well ok, so functor, not function... and it wasn't actually supposed to
do anything was it?

--
Lionel B
 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      12-15-2006
Lionel B wrote:
> On Fri, 15 Dec 2006 14:57:51 +1100, benben wrote:
>
>> How do you declare a function f that takes a parameter that is a pointer
>> to itself?
>>
>> So you can do f(f);

>
> struct ftype
> {
> void operator() (ftype) const {}
> };
>
> int main() {
> ftype f;
> f(f);
> }
>
> Well ok, so functor, not function... and it wasn't actually supposed to
> do anything was it?
>


No it's just I am a bit curios about this, nothing big deal at all. I'm
thinking something like

void f( PTR_TO_FUNC inner)
{
if (inner == 0)
return;

if (...) inner(0);
else inner(inner);
}

Ya I know it's trivial with functor, but I'm just so curios if anyone
can do it with a straight function

Ben
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Declare a Friend function which is a template member function of adifferent class getrakesh@gmail.com C++ 2 02-26-2008 11:32 AM
how do you declare pointer to such nested member function? Mike Wahler C++ 4 03-25-2007 09:44 AM
forward declare member function so that it can be friend function yancheng.cheok@gmail.com C++ 1 11-09-2006 11:30 AM
how to declare pseudo-matrix function argument that is not modified by function? classicist C Programming 3 10-05-2005 02:45 AM



Advertisments