fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <typename Ref, typename T1, typename... TN>
  5. struct all_match;
  6.  
  7. template <typename Ref, typename T>
  8. struct all_match<Ref,T>
  9. {
  10. static constexpr bool value = std::is_same<T,Ref>::value;
  11. };
  12.  
  13. template <typename Ref, typename T1, typename... TN>
  14. struct all_match
  15. {
  16. static constexpr bool value = std::is_same<T1,Ref>::value && all_match<Ref, TN...>::value;
  17. };
  18.  
  19. template <typename T, typename... U>
  20. typename std::enable_if<all_match<int, U...>::value, void>::
  21. type testFunction(T a, U... bs) {
  22. std::cout << "bs are integers\n";
  23. }
  24.  
  25. template <typename T, typename... U>
  26. typename std::enable_if<all_match<char, U...>::value, void>::
  27. type testFunction(T a, U... bs) {
  28. std::cout << "bs are chars\n";
  29. }
  30.  
  31. int main() {
  32. testFunction(1.0, 3); // ok
  33. testFunction(1.0, 'c', 'r', 'p'); // ok
  34. // testFunction(1.0, 3, 4.0f, 5); // fail
  35. return 0;
  36. }
Success #stdin #stdout 0s 4372KB
stdin
Standard input is empty
stdout
bs are integers
bs are chars