fork(4) download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. //this goes in some header so you can use it everywhere
  5. template<typename T>
  6. struct TypeSink{
  7. using Type = void;
  8. };
  9. template<typename T>
  10. using TypeSinkT = typename TypeSink<T>::Type;
  11.  
  12. //use case
  13. template<typename T, typename=void>
  14. struct HasBarOfTypeInt : std::false_type{};
  15. template<typename T>
  16. struct HasBarOfTypeInt<T, TypeSinkT<decltype(std::declval<T&>().*(&T::bar))>> : std::is_same<typename std::decay<decltype(std::declval<T&>().*(&T::bar))>::type,int>{};
  17.  
  18. struct S{
  19. int bar;
  20. };
  21. struct K{
  22.  
  23. };
  24.  
  25. template<typename T, typename = TypeSinkT<decltype(&T::bar)>>
  26. void print(T){
  27. std::cout << "has bar" << std::endl;
  28. }
  29. void print(...){
  30. std::cout << "no bar" << std::endl;
  31. }
  32.  
  33. int main(){
  34. print(S{});
  35. print(K{});
  36. std::cout << "bar is int: " << HasBarOfTypeInt<S>::value << std::endl;
  37. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
has bar
no bar
bar is int: 1