simplename
simplename

Reputation: 919

boost ref of an instance of a class assigned to boost function

I came across the following code where Foo is any user-defined class:

boost::function<return_type (parameter)> f = boost::ref(instanceOfFoo);

And I have the following Qs: 1. What happens if you assign an instance of a class to the boost function pointer? I thought we could only assign functions to it. 2. Why is the boost ref sent and not just the value?

Upvotes: 0

Views: 115

Answers (1)

rhashimoto
rhashimoto

Reputation: 15841

  1. boost::function (and C++11's std::function) wrap callable objects, which are any object that can be invoked like a function. This includes both ordinary functions and functors. A functor is a class or struct that that defines operator().

  2. The boost::function documentation explains why you might want to wrap a reference to a functor instead of the function itself:

In some cases it is expensive (or semantically incorrect) to have Boost.Function clone a function object. In such cases, it is possible to request that Boost.Function keep only a reference to the actual function object. This is done using the ref and cref functions to wrap a reference to a function object:

Upvotes: 1

Related Questions