fork(14) download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. template <typename T, typename U>
  5. struct helper : helper<T, decltype(&U::operator())>
  6. {};
  7.  
  8. template <typename T, typename C, typename R, typename... A>
  9. struct helper<T, R(C::*)(A...) const>
  10. {
  11. static const bool value = std::is_convertible<T, R(*)(A...)>::value;
  12. };
  13.  
  14. template<typename T>
  15. struct is_stateless
  16. {
  17. static const bool value = helper<T,T>::value;
  18. };
  19.  
  20. int main()
  21. {
  22. int a;
  23. auto l1 = [a](){ return 1; };
  24. auto l2 = [](){ return 2; };
  25. auto l3 = [&a](){ return 2; };
  26.  
  27. std::cout << std::boolalpha << is_stateless<decltype(l1)>::value << "\n";
  28. std::cout << std::boolalpha << is_stateless<decltype(l2)>::value << "\n";
  29. std::cout << std::boolalpha << is_stateless<decltype(l3)>::value << "\n";
  30. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
false
true
false