References to functions ?

Discussion in 'C++' started by Timothy Madden, Dec 21, 2004.

  1. Hello

    I have recently read in a fairy good book ('C++ Templates: The Complete
    Guide', by
    David Vandevoorde, Nicolai M. Josuttis) that "both references to objects and
    references to functions are acceptable" as template parameters.

    My question is: Is there such thing as a reference to a function ? What
    exactly does it mean and how could I use such a reference? Has someone used
    this before ?

    Thank you
    "Timothy Madden"
    Romania
    Timothy Madden, Dec 21, 2004
    #1
    1. Advertising

  2. Timothy Madden

    Tom Widmer Guest

    Timothy Madden wrote:
    > Hello
    >
    > I have recently read in a fairy good book ('C++ Templates: The Complete
    > Guide', by
    > David Vandevoorde, Nicolai M. Josuttis) that "both references to objects and
    > references to functions are acceptable" as template parameters.
    >
    > My question is: Is there such thing as a reference to a function ? What
    > exactly does it mean and how could I use such a reference? Has someone used
    > this before ?


    A reference to a function is no different to any other reference. e.g.

    int f(int);

    int (*fptr)(int) = f; //or &f
    int (&fref)(int) = f; //fref isn't assignable, since functions aren't.

    int i = f(10);
    int j = fptr(10); //or int j = (*fptr)(10)
    int k = fref(10);

    So you use a function reference where ever you want a reference to a
    function, as opposed to a pointer to a function, or the function itself.

    Tom
    Tom Widmer, Dec 21, 2004
    #2
    1. Advertising

  3. Timothy Madden

    void Guest

    Timothy Madden wrote:
    > Hello
    >
    > I have recently read in a fairy good book ('C++ Templates: The Complete
    > Guide', by
    > David Vandevoorde, Nicolai M. Josuttis) that "both references to objects and
    > references to functions are acceptable" as template parameters.
    >
    > My question is: Is there such thing as a reference to a function ? What
    > exactly does it mean and how could I use such a reference? Has someone used
    > this before ?
    >


    Consider this example:

    template<typename fun_object>
    void Foo(fun_object &f, void *data)
    {
    f(data);
    }

    void some_function(void *data)
    {
    // does something with data
    }

    class FunObj
    {
    public:
    void operator()(void *data)
    {
    // does something with data
    }
    };

    int main()
    {
    void *data;
    FunObj f;
    Foo(some_function, data);
    Foo(f, data);
    return 0;
    }

    Best
    Darek
    void, Dec 21, 2004
    #3
  4. Tom Widmer wrote:
    > [...]
    > So you use a function reference where ever you want a reference to a
    > function, as opposed to a pointer to a function, or the function itself.


    "As opposed"? You mean that you wouldn't use a function pointer where
    ever you want a pointer to a function (as opposed to a reference)?

    And could you please explain the first part of that sentence? "Use
    a function reference where ever you want a reference to a function"?
    What does that mean? Is the meaning the same as in "use an int pointer
    wherever you want a pointer to int"? Would you say that it's the same
    as "use a bar stool wherever you need a stool in a bar"?

    I am not a native English speaker, you see, that's why I am asking.

    Thanks!

    V
    Victor Bazarov, Dec 21, 2004
    #4
  5. void wrote:
    > Timothy Madden wrote:
    >
    >> Hello
    >>
    >> I have recently read in a fairy good book ('C++ Templates: The Complete
    >> Guide', by
    >> David Vandevoorde, Nicolai M. Josuttis) that "both references to
    >> objects and
    >> references to functions are acceptable" as template parameters.
    >>
    >> My question is: Is there such thing as a reference to a function ? What
    >> exactly does it mean and how could I use such a reference? Has someone
    >> used
    >> this before ?
    >>

    >
    > Consider this example:
    >
    > template<typename fun_object>
    > void Foo(fun_object &f, void *data)


    Here is my question: except due to a mistake, why would one want to have
    the '&' here in the first argument? Why not just write

    template<typename fun_object>
    void Foo(fun_object f, void *data)

    ???

    > {
    > f(data);
    > }
    >
    > void some_function(void *data)
    > {
    > // does something with data
    > }
    >
    > class FunObj
    > {
    > public:
    > void operator()(void *data)
    > {
    > // does something with data
    > }
    > };
    >
    > int main()
    > {
    > void *data;
    > FunObj f;
    > Foo(some_function, data);
    > Foo(f, data);
    > return 0;
    > }


    Yes, in your example, a reference to a function is formed (likely due to
    some mistake in the argument declaration). And, yes, it's _legal_. The
    question remains, however, why would one _need_ to use a reference to
    a function?

    Thanks.

    V
    Victor Bazarov, Dec 21, 2004
    #5
  6. "Victor Bazarov" <> wrote in message
    news:IcXxd.12517$09.us.to.verio.net...
    > void wrote:
    > > Timothy Madden wrote:
    > >
    > >> Hello
    > >>
    > >> I have recently read in a fairy good book ('C++ Templates: The Complete
    > >> Guide', by
    > >> David Vandevoorde, Nicolai M. Josuttis) that "both references to
    > >> objects and
    > >> references to functions are acceptable" as template parameters.
    > >>
    > >> My question is: Is there such thing as a reference to a function ? What
    > >> exactly does it mean and how could I use such a reference? Has someone
    > >> used
    > >> this before ?
    > >>

    > >
    > > Consider this example:
    > >
    > > template<typename fun_object>
    > > void Foo(fun_object &f, void *data)

    >
    > Here is my question: except due to a mistake, why would one want to have
    > the '&' here in the first argument? Why not just write
    >
    > template<typename fun_object>
    > void Foo(fun_object f, void *data)
    >
    > ???

    Please do not be so mad. The poster really answered my question and helped
    me.

    I could want to have the '&' in the first argument if my class in the actual
    parameter is not copy-contructable or if it represents or consumes some
    external resource and is designed with RAII so the constructor allocates
    resources or for the case my fun_object class is a single-ton class. And
    there is allways, of course, the case when my fun_object class is huge and I
    do not need it copied for the purpose of function Foo

    "Timothy Madden"
    Romania
    Timothy Madden, Dec 21, 2004
    #6
  7. Timothy Madden wrote:
    > "Victor Bazarov" <> wrote in message
    > news:IcXxd.12517$09.us.to.verio.net...
    >
    >>void wrote:
    >>
    >>>Timothy Madden wrote:
    >>>
    >>>
    >>>>Hello
    >>>>
    >>>>I have recently read in a fairy good book ('C++ Templates: The Complete
    >>>>Guide', by
    >>>>David Vandevoorde, Nicolai M. Josuttis) that "both references to
    >>>>objects and
    >>>>references to functions are acceptable" as template parameters.
    >>>>
    >>>>My question is: Is there such thing as a reference to a function ? What
    >>>>exactly does it mean and how could I use such a reference? Has someone
    >>>>used
    >>>>this before ?
    >>>>
    >>>
    >>>Consider this example:
    >>>
    >>>template<typename fun_object>
    >>>void Foo(fun_object &f, void *data)

    >>
    >>Here is my question: except due to a mistake, why would one want to have
    >>the '&' here in the first argument? Why not just write
    >>
    >> template<typename fun_object>
    >> void Foo(fun_object f, void *data)
    >>
    >>???

    >
    > Please do not be so mad.


    Mad? Do I really come across as mad? I am sorry. It was by no means my
    intention. I am trying to learn C++ just like all of us here. That's why
    I asked. Perhaps I _am_ mad if my hopes are so high :)

    > The poster really answered my question and helped
    > me.
    >
    > I could want to have the '&' in the first argument if my class in the actual
    > parameter is not copy-contructable or if it represents or consumes some
    > external resource and is designed with RAII so the constructor allocates
    > resources or for the case my fun_object class is a single-ton class. And
    > there is allways, of course, the case when my fun_object class is huge and I
    > do not need it copied for the purpose of function Foo


    Hey, that's a very good explanation. Thank you. I've not considered the
    use of functors that are not copy-constructible.

    V
    Victor Bazarov, Dec 21, 2004
    #7
    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. Roger Leigh
    Replies:
    8
    Views:
    434
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    447
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    601
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    710
    Bruno Desthuilliers
    Dec 12, 2006
  5. Lars Willich
    Replies:
    13
    Views:
    833
    Ian Shef
    Oct 23, 2007
Loading...

Share This Page