expensive_copy_local_function.cpp 941 B

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright (C) 2009-2012 Lorenzo Caminiti
  2. // Distributed under the Boost Software License, Version 1.0
  3. // (see accompanying file LICENSE_1_0.txt or a copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Home at http://www.boost.org/libs/local_function
  6. #include <boost/local_function.hpp>
  7. #include <boost/typeof/typeof.hpp>
  8. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  9. #include <iostream>
  10. #include <cassert>
  11. //[expensive_copy_local_function
  12. struct n {
  13. int i;
  14. n(int _i): i(_i) {}
  15. n(n const& x): i(x.i) { // Some time consuming copy operation.
  16. for (unsigned i = 0; i < 10000; ++i) std::cout << '.';
  17. }
  18. };
  19. BOOST_TYPEOF_REGISTER_TYPE(n) // Register for `bind& x` below.
  20. int main(void) {
  21. n x(-1);
  22. void BOOST_LOCAL_FUNCTION(const bind& x) { // OK: No copy expensive
  23. assert(x.i == -1); // copy but constant.
  24. } BOOST_LOCAL_FUNCTION_NAME(f)
  25. f();
  26. return 0;
  27. }
  28. //]