fork download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. template < typename T >
  5. void foo( const T& arg ) {
  6.  
  7. using namespace std;
  8.  
  9. static_assert( is_same<int,T>::value |
  10. is_same<long,T>::value |
  11. is_same<std::string,T>::value , "only accept int, long and std::string as argument types" );
  12.  
  13. cout << "foo: ";
  14. if( is_same<int,T>::value ) {
  15. cout << "<int> " << arg << endl;
  16. } else if( is_same<long,T>::value ) {
  17. cout << "<long> " << arg << endl;
  18. } else if( is_same<std::string,T>::value ) {
  19. cout << "<string> " << arg << endl;
  20. }
  21. }
  22.  
  23. int main() {
  24.  
  25. foo( 3u );
  26. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘void foo(const T&) [with T = unsigned int]’:
prog.cpp:25:13:   required from here
prog.cpp:9:5: error: static assertion failed: only accept int, long and std::string as argument types
stdout
Standard output is empty