fork download
  1. #include <iostream>
  2. #include <typeinfo>
  3.  
  4. template <typename> struct trait;
  5.  
  6. template <> struct trait<char> {
  7. using storage = char;
  8. };
  9.  
  10. template <> struct trait<int> {
  11. using storage = double;
  12. };
  13.  
  14. template <> struct trait<float> {
  15. // No storage
  16. };
  17.  
  18. template <typename> struct void_t { using type = void; };
  19.  
  20. template <typename T, typename = void>
  21. struct example_user
  22. {
  23. static void do_something() {
  24. std::cout << "invoked on " << typeid(T).name() << " which doesn't have storage" << std::endl;
  25. }
  26. };
  27.  
  28. template <typename T>
  29. struct example_user<T, typename void_t<typename trait<T>::storage>::type>
  30. {
  31. static void do_something() {
  32. std::cout << "invoked on " << typeid(T).name() << " which does have storage of type "
  33. << typeid(typename trait<T>::storage).name()
  34. << std::endl;
  35. }
  36. };
  37.  
  38. int main() {
  39. example_user<char>::do_something();
  40. example_user<int>::do_something();
  41. example_user<float>::do_something();
  42. return 0;
  43. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
invoked on c which does have storage of type c
invoked on i which does have storage of type d
invoked on f which doesn't have storage