fork download
  1. #include <iostream>
  2. #include <type_traits> // <-------
  3. using namespace std;
  4.  
  5. template<typename T>
  6. enable_if_t<is_fundamental<T>::value, T>
  7. sum(const T& a, const T& b) { return a + b;}
  8.  
  9. template<typename T>
  10. enable_if_t<!is_fundamental<T>::value, T>
  11. sum(const T&, const T&) { static_assert(false, "Not fundamental type");}
  12.  
  13. int main()
  14. {
  15. struct A {};
  16. cout << sum(5.1f, 9.4f) << endl;
  17. sum(A(), A());
  18. return 0;
  19. }
  20.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'std::enable_if_t<(! std::is_fundamental<_Tp>::value), T> sum(const T&, const T&)':
prog.cpp:11:27: error: static assertion failed: Not fundamental type
 sum(const T&, const T&) { static_assert(false, "Not fundamental type");}
                           ^
stdout
Standard output is empty