fork download
  1. #include <type_traits>
  2.  
  3. template<class As>
  4. struct any_char_ptr
  5. {
  6. static const bool is_const = std::is_const<As>::value;
  7.  
  8. typedef typename std::conditional<is_const,const char,char>::type
  9. plain_char_type;
  10. typedef typename std::conditional<is_const,const signed char,signed char>::type
  11. signed_char_type;
  12. typedef typename std::conditional<is_const,const unsigned char,unsigned char>::type
  13. unsigned_char_type;
  14.  
  15. As* ptr;
  16.  
  17. any_char_ptr(plain_char_type* p)
  18. : ptr(reinterpret_cast<As*>(p)) {}
  19. any_char_ptr(signed_char_type* p)
  20. : ptr(reinterpret_cast<As*>(p)) {}
  21. any_char_ptr(unsigned_char_type* p)
  22. : ptr(reinterpret_cast<As*>(p)) {}
  23.  
  24. operator As*() const {return ptr;}
  25. };
  26.  
  27. #include <iostream>
  28.  
  29. void foo(any_char_ptr<unsigned char> p)
  30. {
  31. std::cout << unsigned(*p) << std::endl;
  32. }
  33.  
  34. int main()
  35. {
  36. char c=65; signed char s=66; unsigned char u=67;
  37. foo(&c);
  38. foo(&s);
  39. foo(&u);
  40. }
  41.  
Success #stdin #stdout 0s 2928KB
stdin
Standard input is empty
stdout
65
66
67