References to functions ? | | |
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 | | | | re: References to functions ?
Timothy Madden wrote:[color=blue]
> 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 ?[/color]
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 | | | | re: References to functions ?
Timothy Madden wrote:[color=blue]
> 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 ?
>[/color]
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 | | | | re: References to functions ?
Tom Widmer wrote:[color=blue]
> [...]
> 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.[/color]
"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 | | | | re: References to functions ?
void wrote:[color=blue]
> Timothy Madden wrote:
>[color=green]
>> 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 ?
>>[/color]
>
> Consider this example:
>
> template<typename fun_object>
> void Foo(fun_object &f, void *data)[/color]
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)
???
[color=blue]
> {
> 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;
> }[/color]
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 | | | | re: References to functions ?
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:IcXxd.12517$Ae.8647@newsread1.dllstx09.us.to. verio.net...[color=blue]
> void wrote:[color=green]
> > Timothy Madden wrote:
> >[color=darkred]
> >> 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 ?
> >>[/color]
> >
> > Consider this example:
> >
> > template<typename fun_object>
> > void Foo(fun_object &f, void *data)[/color]
>
> 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)
>
> ???[/color]
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 | | | | re: References to functions ?
Timothy Madden wrote:[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> news:IcXxd.12517$Ae.8647@newsread1.dllstx09.us.to. verio.net...
>[color=green]
>>void wrote:
>>[color=darkred]
>>>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)[/color]
>>
>>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)
>>
>>???[/color]
>
> Please do not be so mad.[/color]
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 :-)
[color=blue]
> 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[/color]
Hey, that's a very good explanation. Thank you. I've not considered the
use of functors that are not copy-constructible.
V |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,327 network members.
|