Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

C++11 adopted strongly-typed enums, but I wish it also added strongly-typed typedefs. I've written (and found others') template classes to implement something similar, but the implementation is surprisingly (?) verbose and may introduce code bloat or runtime overhead. The compiler could implement this with no overhead!

C++11's strongly-typed enums look like:

  enum class Enumeration { Val1, Val2};
  enum class Enum2 : unsigned int {Val1, Val2};
A strongly-typed typedef could use similar "declare a class deriving from a native type" syntax:

  class Celcius : double;      // something like this?
  typedef Fahrenheit : double; // or maybe this?
  Celcius celcius = 0f;
  Fahrenheit fahrenheit = celcius; // ERROR


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: