fork download
  1. #include <iostream>
  2.  
  3. struct hoge_tag {};
  4. struct foo_tag {};
  5.  
  6. struct hoge { typedef hoge_tag call_tag; };
  7. struct foo { typedef foo_tag call_tag; };
  8.  
  9. template<class T> struct call_traits {
  10. typedef T call_tag;
  11. };
  12. template<> struct call_traits<hoge> {
  13. typedef hoge::call_tag call_tag;
  14. };
  15. template<> struct call_traits<foo> {
  16. typedef foo::call_tag call_tag;
  17. };
  18.  
  19. namespace detail {
  20. void call_(hoge_tag) { std::cout << "hoge." << std::endl; };
  21. void call_(foo_tag) { std::cout << "foo." << std::endl; };
  22. template<class T>
  23. void call_(T) { std::cout << "other." << std::endl; };
  24. };
  25.  
  26. template<class T>
  27. void call(T) {
  28. typedef typename call_traits<T>::call_tag call_tag;
  29. detail::call_(call_tag());
  30. }
  31.  
  32. int main() {
  33. call(hoge()); // "hoge."
  34. call(foo()); // "foo."
  35. call(int()); // "other."
  36. }
  37.  
Success #stdin #stdout 0s 2828KB
stdin
Standard input is empty
stdout
hoge.
foo.
other.