typeof_template.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 "addable.hpp"
  11. #include <boost/local_function.hpp>
  12. #include <boost/type_traits/remove_reference.hpp>
  13. #include <boost/concept_check.hpp>
  14. #include <boost/detail/lightweight_test.hpp>
  15. #include <algorithm>
  16. //[typeof_template
  17. template<typename T>
  18. T calculate(const T& factor) {
  19. T sum = 0;
  20. void BOOST_LOCAL_FUNCTION_TPL(const bind factor, bind& sum, T num) {
  21. // Local function `TYPEOF` does not need `typename`.
  22. BOOST_CONCEPT_ASSERT((Addable<typename boost::remove_reference<
  23. BOOST_LOCAL_FUNCTION_TYPEOF(sum)>::type>));
  24. sum += factor * num;
  25. } BOOST_LOCAL_FUNCTION_NAME_TPL(add)
  26. add(6);
  27. return sum;
  28. }
  29. //]
  30. int main(void) {
  31. BOOST_TEST(calculate(10) == 60);
  32. return boost::report_errors();
  33. }
  34. #endif // VARIADIC_MACROS