How can you declare function f that...

Discussion in 'C++' started by benben, Dec 15, 2006.

  1. benben

    benben Guest

    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
    benben, Dec 15, 2006
    #1
    1. Advertising

  2. 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 );
    }
    Vyacheslav Kononenko, Dec 15, 2006
    #2
    1. Advertising

  3. benben

    softcoder Guest

    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 );
    > }
    softcoder, Dec 15, 2006
    #3
  4. benben

    John Carson Guest

    "softcoder" <> wrote in message
    news:
    >
    > 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
    John Carson, Dec 15, 2006
    #4
  5. benben

    Lionel B Guest

    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
    Lionel B, Dec 15, 2006
    #5
  6. benben

    benben Guest

    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 :D

    Ben
    benben, Dec 15, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul
    Replies:
    1
    Views:
    408
    Adam Maass
    Aug 14, 2003
  2. classicist
    Replies:
    3
    Views:
    321
  3. Replies:
    1
    Views:
    1,307
    Jim Langston
    Nov 9, 2006
  4. Mike Wahler
    Replies:
    4
    Views:
    327
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Mar 25, 2007
  5. Replies:
    2
    Views:
    656
    Triple-DES
    Feb 26, 2008
Loading...

Share This Page