fork(1) download
  1. #include <iostream>
  2. #include <utility>
  3.  
  4. using namespace std;
  5.  
  6. template <typename T, typename Enable = typename std::conditional<!std::is_fundamental<T>::value,
  7. std::true_type,
  8. std::false_type>::type>
  9. struct strong_typedef;
  10.  
  11. //specialization for non fundamental types
  12. template <typename T>
  13. struct strong_typedef<T, std::true_type> : public T
  14. {
  15. template <typename... Args>
  16. strong_typedef(Args&&... args) : T(std::forward<Args>(args)...) {}
  17. };
  18.  
  19. //specialization for fundamental types
  20. template <typename T>
  21. struct strong_typedef<T, std::false_type>
  22. {
  23. T t;
  24. explicit strong_typedef(const T t_) : t(t_) {};
  25. strong_typedef(){};
  26. strong_typedef(const strong_typedef & t_) : t(t_.t){}
  27. strong_typedef & operator=(const strong_typedef & rhs) { t = rhs.t; return *this;}
  28. strong_typedef & operator=(const T & rhs) { t = rhs; return *this;}
  29. /*operator const T & () const {return t; }*/
  30. operator T & () { return t; }
  31. bool operator==(const strong_typedef & rhs) const { return t == rhs.t; }
  32. bool operator<(const strong_typedef & rhs) const { return t < rhs.t; }
  33. };
  34.  
  35. struct A {int value;};
  36. using B = strong_typedef<A>;
  37.  
  38. int main() {
  39. B test;
  40. test.value = 4;
  41. std::cout << test.value << std::endl;
  42. return 0;
  43. }
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
4