Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2004-04-12 08:56:10


Thorsten Ottosen wrote:
>
> My point is that if the prototype was
>
> void square( int& );
>
> then
>
> function< void(void) > f( int x )
> {
> return bind( square, x*5 );
> }
>
> should not compile.

Why should it not compile?

int increment(int & x) { return ++x; }

template<class F> void test_nonconst(F f)
{
    std::cout << f() << '\n';
    std::cout << f() << '\n';
    std::cout << f() << '\n';
}

template<class F> void test_const(F const & f)
{
    std::cout << f() << '\n';
    std::cout << f() << '\n';
    std::cout << f() << '\n';
}

int main()
{
    test_nonconst( bind(increment, 0) ); // 1 2 3
    test_const( bind(increment, 0) ); // compile-fail
}

It's as if you've written

struct increment
{
    int a1_;
    int operator()() { return ++a1_; }
};

yourself.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk