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