fork download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. template < typename T >
  5. void foo( const T& arg ) {
  6. using namespace std;
  7.  
  8. cout << "foo: ";
  9. if( is_same<int,T>::value ) {
  10. cout << "<int> " << arg << endl;
  11. } else if( is_same<long,T>::value ) {
  12. cout << "<long> " << arg << endl;
  13. } else if( is_same<std::string,T>::value ) {
  14. cout << "<string> " << arg << endl;
  15. } else {
  16. cout << "<other> " << arg << endl;
  17. }
  18. }
  19.  
  20. int main() {
  21.  
  22. foo( 1 );
  23. foo( 2L );
  24. foo( 3u );
  25. foo( std::string("hello world") );
  26. }
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
foo: <int> 1
foo: <long> 2
foo: <other> 3
foo: <string> hello world