Artificial typification of homogeneous arguments in C++

Let’s say there is a class with following interface:

class Date {
 public:
  Date(int year, int month, int day) {
    ...
  }
};

Unfortunately not everybody in the world uses the same quite logical notation of Year/Month/Day or Day/Month/Year. Some people prefer Month/Day/Year. But even the first two could be easily mixed up. If the following is written:

Data d(2009, 4, 5);

Is it 4th of May or 5th of April? Who can be entirely sure what it exactly means without looking in the class declaration?

Any chance to improve the design? Indeed.

For instance:

class Year {
 public:
  explicit Year(int year) : year_(year) {}
  operator int() const { return year_; }
 private:
  int year_;
};

And similar for the rest:

class Month { ... };
class Day { ... };

Now the interface could look this way:

class Date {
 public:
   Date(Year year, Month month, Day day);
   Date(Month month, Day day, Year year);
   Date(Day day, Month month, Year year);
}

We can instantiate the class as:

Date d(Year(2010), Month(4), Day(5));

or

Date d(Month(4), Day(5), Year(2010));

The result is always visible and fully predictable in a calling code. Everything will be inlined so no slow down involved at all because of those three “unnecessary” classes.

I agree there is more typing job but it fully gets you rid of any typos and consequently of silly but annoying bugs.

Any objections?


Original post | Disclaimer

Comments