expensive_copy_cxx11_lambda.cpp 840 B

123456789101112131415161718192021222324252627282930313233343536373839
  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/config.hpp>
  7. #ifdef BOOST_NO_CXX11_LAMBDAS
  8. # error "lambda functions required"
  9. #else
  10. #include <iostream>
  11. #include <cassert>
  12. //[expensive_copy_cxx11_lambda
  13. struct n {
  14. int i;
  15. n(int _i): i(_i) {}
  16. n(n const& x): i(x.i) { // Some time consuming copy operation.
  17. for (unsigned i = 0; i < 10000; ++i) std::cout << '.';
  18. }
  19. };
  20. int main(void) {
  21. n x(-1);
  22. auto f = [x]() { // Problem: Expensive copy, but if bind
  23. assert(x.i == -1); // by `&x` then `x` is not constant.
  24. };
  25. f();
  26. return 0;
  27. }
  28. //]
  29. #endif // NO_LAMBDAS