fork(1) download
  1. #include <vector>
  2. #include <utility>
  3. #include <type_traits>
  4. #include <iostream>
  5. #include <iterator>
  6.  
  7. template <typename Iter>
  8. using iter_val_t = typename std::iterator_traits< Iter >::value_type;
  9.  
  10. template <typename Iter, typename T>
  11. constexpr bool is_T_iter_v = std::is_same< iter_val_t< Iter >, T >::value;
  12.  
  13. template <typename Iter>
  14. constexpr bool is_wchar_iter_v = std::is_same< iter_val_t< Iter >, wchar_t >::value;
  15.  
  16. template <typename Iter, std::enable_if_t < is_T_iter_v< Iter, char >, int > = 0 >
  17. void CalcSomething( Iter begin, Iter end )
  18. {
  19. std::cout << "i'm char\n";
  20. }
  21.  
  22. template <typename Iter, std::enable_if_t < is_T_iter_v< Iter, char16_t > ||
  23. ( is_wchar_iter_v< Iter > && sizeof( wchar_t ) == sizeof( char16_t ) ), int > = 0 >
  24. void CalcSomething( Iter begin, Iter end )
  25. {
  26. std::cout << "i'm char16\n";
  27. }
  28.  
  29. template <typename Iter, std::enable_if_t < is_T_iter_v< Iter, char32_t > ||
  30. ( is_wchar_iter_v< Iter > && sizeof( wchar_t ) == sizeof( char32_t ) ), int > = 0 >
  31. void CalcSomething( Iter begin, Iter end )
  32. {
  33. std::cout << "i'm char32\n";
  34. }
  35.  
  36.  
  37. int main()
  38. {
  39. const char arr[ 5 ] = {};
  40. CalcSomething( std::begin( arr ), std::end( arr ) );
  41.  
  42. std::vector<char> v1;
  43. CalcSomething( v1.begin(), v1.end() );
  44.  
  45. std::vector<char16_t> v2;
  46. CalcSomething( v2.begin(), v2.end() );
  47.  
  48. std::vector<char32_t> v3;
  49. CalcSomething( v3.begin(), v3.end() );
  50.  
  51. std::vector<wchar_t> v4;
  52. CalcSomething( v4.begin(), v4.end() );
  53. }
Success #stdin #stdout 0s 4704KB
stdin
Standard input is empty
stdout
i'm char
i'm char
i'm char16
i'm char32
i'm char32