function_arith_cxx98.cpp 798 B

12345678910111213141516171819202122232425262728293031323334
  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 <iostream>
  9. float mul_ints(int x, int y) { return ((float)x) * y; }
  10. struct int_div {
  11. float operator()(int x, int y) const { return ((float)x)/y; };
  12. };
  13. int main()
  14. {
  15. boost::function<float (int x, int y)> f;
  16. f = int_div();
  17. std::cout << f(5, 3) << std::endl;
  18. if (f)
  19. std::cout << f(5, 3) << std::endl;
  20. else
  21. std::cout << "f has no target, so it is unsafe to call" << std::endl;
  22. f = 0;
  23. f = &mul_ints;
  24. return 0;
  25. }