mem_fun_portable.cpp 944 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Function library
  2. // Copyright (C) 2001-2003 Douglas Gregor
  3. // Use, modification and distribution is subject to the Boost Software
  4. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // For more information, see http://www.boost.org/
  7. #include <boost/function.hpp>
  8. #include <boost/core/lightweight_test.hpp>
  9. #include <iostream>
  10. #include <functional>
  11. struct Y {
  12. Y(int y = 0) : y_(y) {}
  13. bool operator==(const Y& rhs) { return y_ == rhs.y_; }
  14. private:
  15. int y_;
  16. };
  17. struct X {
  18. int foo(int);
  19. Y& foo2(Y&) const;
  20. };
  21. int X::foo(int x) { return -x; }
  22. Y& X::foo2(Y& x) const { return x; }
  23. int main()
  24. {
  25. boost::function2<int, X*, int> f;
  26. boost::function2<Y&, X*, Y&> f2;
  27. Y y1;
  28. f = &X::foo;
  29. f2 = &X::foo2;
  30. X x;
  31. BOOST_TEST(f(&x, 5) == -5);
  32. BOOST_TEST(f2(&x, boost::ref(y1)) == y1);
  33. return ::boost::report_errors();
  34. }