fork download
  1. #include <type_traits>
  2.  
  3. template<typename U, typename L>
  4. static auto Update(volatile U &state, const L &lambda) ->
  5. //typename std::result_of<L(U,U)>::type
  6. decltype(lambda(*(U*)0,*(U*)0))
  7. {
  8. typedef decltype(lambda(*(U*)0,*(U*)0)) ResultType;
  9. //typedef typename std::result_of<L(U,U)>::type ResultType;
  10.  
  11. return Update<ResultType,U,L>(state, lambda, (typename std::is_void<ResultType>::type *)0);
  12. }
  13.  
  14. // Specialization for lambda that returns type R (not void)
  15. template<typename R, typename U, typename L>
  16. static R Update(volatile U &state, const L &lambda, const std::false_type *)
  17. {
  18. // Real code does cmpxchg loop
  19. return state;
  20. }
  21.  
  22. template<typename R, typename U, typename L>
  23. static void Update(volatile U &state, const L &lambda, const std::true_type *)
  24. {
  25. // Real code does cmpxchg loop
  26. }
  27.  
  28. int main()
  29. {
  30. int x;
  31. auto shouldBeBool = Update(x,
  32. [](int oldvalue, int &newvalue)
  33. {
  34. newvalue = oldvalue + 1;
  35. return true;
  36. });
  37.  
  38. return (int)std::is_same<bool,decltype(shouldBeBool)>::value;
  39. }
  40.  
Runtime error #stdin #stdout 0s 2848KB
stdin
Standard input is empty
stdout
Standard output is empty