static_if.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*=============================================================================
  2. Copyright (c) 2017 Paul Fultz II
  3. static_if.cpp
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. /*=============================================================================
  8. Copyright (c) 2016 Paul Fultz II
  9. static_if.cpp
  10. Distributed under the Boost Software License, Version 1.0. (See accompanying
  11. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  12. ==============================================================================*/
  13. #include "example.h"
  14. using namespace boost::hof;
  15. // static_if example taken from Baptiste Wicht:
  16. // http://baptiste-wicht.com/posts/2015/07/simulate-static_if-with-c11c14.html
  17. template<typename T>
  18. void decrement_kindof(T& value)
  19. {
  20. eval(first_of(
  21. if_(std::is_same<std::string, T>())([&](auto id){
  22. id(value).pop_back();
  23. }),
  24. [&](auto id){
  25. --id(value);
  26. }
  27. ));
  28. }
  29. int main()
  30. {
  31. std::string s = "hello!";
  32. decrement_kindof(s);
  33. assert(s == "hello");
  34. int i = 4;
  35. decrement_kindof(i);
  36. assert(i == 3);
  37. }