same_line_seq.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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/preprocessor/cat.hpp>
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <iostream>
  10. #define LOCAL_INC_DEC(offset) \
  11. int BOOST_LOCAL_FUNCTION_ID(BOOST_PP_CAT(inc, __LINE__), /* unique ID */ \
  12. (const bind offset) (const int x) ) { \
  13. return x + offset; \
  14. } BOOST_LOCAL_FUNCTION_NAME(inc) \
  15. \
  16. int BOOST_LOCAL_FUNCTION_ID(BOOST_PP_CAT(dec, __LINE__), \
  17. (const bind offset) (const int x) ) { \
  18. return x - offset; \
  19. } BOOST_LOCAL_FUNCTION_NAME(dec)
  20. #define LOCAL_INC_DEC_TPL(offset) \
  21. T BOOST_LOCAL_FUNCTION_ID_TPL(BOOST_PP_CAT(inc, __LINE__), \
  22. (const bind offset) (const T x) ) { \
  23. return x + offset; \
  24. } BOOST_LOCAL_FUNCTION_NAME_TPL(inc) \
  25. \
  26. T BOOST_LOCAL_FUNCTION_ID_TPL(BOOST_PP_CAT(dec, __LINE__), \
  27. (const bind offset) (const T x) ) { \
  28. return x - offset; \
  29. } BOOST_LOCAL_FUNCTION_NAME_TPL(dec)
  30. template<typename T>
  31. void f(T& delta) {
  32. LOCAL_INC_DEC_TPL(delta) // Multiple local functions on same line.
  33. BOOST_TEST(dec(inc(123)) == 123);
  34. }
  35. int main(void) {
  36. int delta = 10;
  37. LOCAL_INC_DEC(delta) // Declare local functions on same line using `_ID`.
  38. BOOST_TEST(dec(inc(123)) == 123);
  39. f(delta);
  40. return boost::report_errors();
  41. }