References to functions ?

T

Timothy Madden

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
 
T

Tom Widmer

Timothy said:
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
 
V

void

Timothy said:
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
 
V

Victor Bazarov

Tom said:
[...]
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
 
V

Victor Bazarov

void said:
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
 
T

Timothy Madden

Victor Bazarov said:
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
 
V

Victor Bazarov

Timothy said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top