fork(1) download
  1. #include <set>
  2. #include <map>
  3. #include <iostream>
  4. #include <typeinfo>
  5.  
  6. //// This is a SFINAE context enabler. If T is defined R is returned
  7. template<class T, class R=void> struct enable_if_type
  8. {
  9. typedef R type;
  10. };
  11.  
  12. //// Default case is undefined as you want to get an error if you try to get a key_type from something that has none
  13. template<class T, class Enable=void> struct key_type_of;
  14.  
  15.  
  16. //// If T::key_type is a valid expression, extract it
  17. template<class T>
  18. struct key_type_of< T
  19. , typename enable_if_type< typename T::key_type>::type
  20. >
  21. {
  22. typedef typename T::key_type type;
  23. };
  24.  
  25. int main()
  26. {
  27. typedef key_type_of< std::set<int> >::type skey;
  28. typedef key_type_of< std::map<std::string,std::string*> >::type mkey;
  29.  
  30. std::cout << typeid(skey).name() << "\n";
  31. std::cout << typeid(mkey).name() << "\n";
  32.  
  33. }
Success #stdin #stdout 0.01s 2680KB
stdin
Standard input is empty
stdout
i
Ss