2003-02-14 18:05:26 +00:00
|
|
|
// Function library
|
|
|
|
|
|
|
|
// Copyright (C) 2001-2003 Douglas Gregor
|
|
|
|
|
2003-10-01 04:10:37 +00:00
|
|
|
// Use, modification and distribution is subject to the Boost Software
|
|
|
|
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
|
|
|
// http://www.boost.org/LICENSE_1_0.txt)
|
2003-02-14 18:05:26 +00:00
|
|
|
|
|
|
|
// For more information, see http://www.boost.org/
|
2002-12-28 03:44:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include <boost/function.hpp>
|
2018-09-22 15:11:12 -04:00
|
|
|
#include <boost/core/lightweight_test.hpp>
|
2002-12-28 03:44:00 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <functional>
|
|
|
|
|
2014-02-03 11:46:05 -08:00
|
|
|
struct Y {
|
|
|
|
Y(int y = 0) : y_(y) {}
|
|
|
|
bool operator==(const Y& rhs) { return y_ == rhs.y_; }
|
|
|
|
private:
|
|
|
|
int y_;
|
|
|
|
};
|
|
|
|
|
2002-12-28 03:44:00 +00:00
|
|
|
struct X {
|
|
|
|
int foo(int);
|
2014-02-03 11:46:05 -08:00
|
|
|
Y& foo2(Y&) const;
|
2002-12-28 03:44:00 +00:00
|
|
|
};
|
2003-01-19 14:44:34 +00:00
|
|
|
int X::foo(int x) { return -x; }
|
2014-02-03 11:46:05 -08:00
|
|
|
Y& X::foo2(Y& x) const { return x; }
|
2003-01-19 14:44:34 +00:00
|
|
|
|
2002-12-28 03:44:00 +00:00
|
|
|
int main()
|
|
|
|
{
|
2003-02-14 18:05:26 +00:00
|
|
|
boost::function<int (X*, int)> f;
|
2014-02-03 11:46:05 -08:00
|
|
|
boost::function<Y& (X*, Y&)> f2;
|
|
|
|
Y y1;
|
|
|
|
|
2011-03-21 21:32:38 +00:00
|
|
|
f = &X::foo;
|
|
|
|
f2 = &X::foo2;
|
2002-12-28 03:44:00 +00:00
|
|
|
|
2011-03-21 21:32:38 +00:00
|
|
|
X x;
|
|
|
|
BOOST_TEST(f(&x, 5) == -5);
|
2014-02-03 11:46:05 -08:00
|
|
|
BOOST_TEST(f2(&x, boost::ref(y1)) == y1);
|
2011-03-21 21:32:38 +00:00
|
|
|
|
|
|
|
return ::boost::report_errors();
|
2002-12-28 03:44:00 +00:00
|
|
|
}
|