same_line.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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_VARIADIC_MACROS
  8. # error "variadic macros required"
  9. #else
  10. #include <boost/local_function.hpp>
  11. #include <boost/preprocessor/cat.hpp>
  12. #include <boost/detail/lightweight_test.hpp>
  13. #include <iostream>
  14. //[same_line
  15. #define LOCAL_INC_DEC(offset) \
  16. int BOOST_LOCAL_FUNCTION_ID(BOOST_PP_CAT(inc, __LINE__), /* unique ID */ \
  17. const bind offset, const int x) { \
  18. return x + offset; \
  19. } BOOST_LOCAL_FUNCTION_NAME(inc) \
  20. \
  21. int BOOST_LOCAL_FUNCTION_ID(BOOST_PP_CAT(dec, __LINE__), \
  22. const bind offset, const int x) { \
  23. return x - offset; \
  24. } BOOST_LOCAL_FUNCTION_NAME(dec)
  25. #define LOCAL_INC_DEC_TPL(offset) \
  26. T BOOST_LOCAL_FUNCTION_ID_TPL(BOOST_PP_CAT(inc, __LINE__), \
  27. const bind offset, const T x) { \
  28. return x + offset; \
  29. } BOOST_LOCAL_FUNCTION_NAME_TPL(inc) \
  30. \
  31. T BOOST_LOCAL_FUNCTION_ID_TPL(BOOST_PP_CAT(dec, __LINE__), \
  32. const bind offset, const T x) { \
  33. return x - offset; \
  34. } BOOST_LOCAL_FUNCTION_NAME_TPL(dec)
  35. template<typename T>
  36. void f(T& delta) {
  37. LOCAL_INC_DEC_TPL(delta) // Multiple local functions on same line.
  38. BOOST_TEST(dec(inc(123)) == 123);
  39. }
  40. int main(void) {
  41. int delta = 10;
  42. LOCAL_INC_DEC(delta) // Multiple local functions on same line.
  43. BOOST_TEST(dec(inc(123)) == 123);
  44. f(delta);
  45. return boost::report_errors();
  46. }
  47. //]
  48. #endif // VARIADIC_MACROS