fork(2) download
  1. //Refer to: http://m...content-available-to-author-only...y.com/2013/06/03/overriding-the-broken-universal-reference-t/
  2. #include <iostream>
  3. #include <type_traits>
  4.  
  5. template<typename T>
  6. struct class_tag { };
  7.  
  8. template<typename TF>
  9. void apply( TF && f ) {
  10. //get the unqualified type for the purpose of tagging
  11. class_tag<typename std::decay<TF>::type> tag;
  12. apply_impl( std::forward<TF>(f), tag );
  13. }
  14.  
  15. template<typename TF, typename Tag>
  16. void apply_impl( TF && f, Tag ) {
  17. std::cout << f << std::endl;
  18. }
  19.  
  20. struct match_a { };
  21. template<typename TF>
  22. void apply_impl( TF && f, class_tag<match_a> ) {
  23. std::cout << "match_a" << std::endl;
  24. }
  25.  
  26. struct match_b { };
  27. template<typename TF>
  28. void apply_impl( TF && f, class_tag<match_b> ) {
  29. std::cout << "match_b" << std::endl;
  30. }
  31.  
  32. template<typename TF>
  33. void apply_impl( TF && f, class_tag<int*> ) {
  34. std::cout << "int*" << std::endl;
  35. }
  36.  
  37. int main() {
  38. apply( 12 );
  39. apply( "hello" );
  40. apply( match_a() );
  41. apply( match_b() );
  42.  
  43. match_a a;
  44. apply(a);
  45. apply( static_cast<match_a const&>(a) );
  46. apply( static_cast<match_a const>(a) );
  47.  
  48. int b[5];
  49. apply(b);
  50. apply(static_cast<int*>(b));
  51. }
  52.  
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
12
hello
match_a
match_b
match_a
match_a
match_a
int*
int*