On reference_wrapper and callable objects

Posted by Nicola Bonelli on Stack Overflow See other posts from Stack Overflow or by Nicola Bonelli
Published on 2010-05-15T14:57:52Z Indexed on 2010/05/15 15:04 UTC
Read the original article Hit count: 308

Filed under:
|
|
|

Given the following callable object:

struct callable : public std::unary_function <void, void>
{
    void
    operator()() const
    {
        std::cout << "hello world" << std::endl;
    }
};  

a std::tr1::reference_wrapper<> calls through it:

callable obj;
std::tr1::ref(obj)();

Instead, when the operator() accepts an argument:

struct callable : public std::unary_function &lt;int, void&gt;
{
    void
    operator()(int n) const
    {
        std::cout << n << std::endl;
    }
};  

std::tr1::bind accepts a reference_wrapper to it as a callable wrapper...

callable obj;
std::tr1::bind( std::tr1::ref(obj), 42 )();

but what's wrong with this?

std::tr1::ref(obj)(42);

© Stack Overflow or respective owner

Related posts about c++

Related posts about tr1