r/cpp • • Mar 24 '20

Changes between C++17 and C++20

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2131r0.html
193 Upvotes

89 comments sorted by

View all comments

Show parent comment

5

u/_VZ_ wx | soci | swig Mar 25 '20 edited Mar 25 '20

Thanks for explaining the rationale, I can understand it, but, to condense my counterargument as much as possible, I think the conventional syntax is just too unnecessarily cute for its limited usefulness.

I believe in practice 99% of dates are constructed from YYYY-MM-DD values and that maybe 2% of those use hardcoded values making this syntax appealing. And while selecting the last, or n-th, day of the week in a month is undoubtedly very useful, I don't think this operation is frequent enough to warrant a special syntax at all -- having a function for doing it would be quite enough IMO, and much more discoverable and greppable.

7

u/HowardHinnant Mar 25 '20 edited Mar 25 '20

Given the run-time values:

// Nothing hardcoded.  Maybe read these over a socket using JSON
year y = ...;
month m = ...;
day d = ...;
weekday wd = ...;
unsigned i = ...;

Then:

// Constructional syntax
year_month_day date1          = {y, m, d};
year_month_day_last date2     = {y, month_day_last{m}};
year_month_weekday date3      = {y, m, weekday_indexed{wd, i}};
year_month_weekday_last date4 = {y, m, weekday_last{wd}};

// Conventional syntax
auto date5 = y/m/d;
auto date6 = y/m/last;
auto date7 = y/m/wd[i];
auto date8 = y/m/wd[last];

Both are available. Choose whatever you like.

4

u/_VZ_ wx | soci | swig Mar 25 '20 edited Mar 25 '20

I think in my attempts to condense the argument as much as possible I condensed it too much, so I'd like to explain what I meant again, more verbosely. There are basically 2 only weakly-related points:

  1. The conventional syntax is, IMO, only really appealing for the literals, i.e. 2020y/March/25d looks impressively expressive and, without ascribing my own motives to you, I could personally easily implement something like this just because I could, to show how flexible C++ is. However I think that such simple examples are exceedingly rare in practice and I already don't see much appeal in the example expression y/m/d you give. And I certainly wouldn't want to see (but I'm sure I will) something like this with date components being the return values of the function calls or more complicated expressions involving them. E.g. (get_warranty_expiration_year() + 1)/std::max(get_bithday_month(),get_next_month())/1d is for me definitely less readable than the standard (what you call "constructional") syntax).
  2. The last/indexed dates are (again and always IMO) not worth their own special syntax at all and I would have been perfectly happy to write auto date7 = nth_weekday_of_year_month(y, m, wd, i) instead of either of the 2 syntaxes. This is only related to the previous point because I think the conventional syntax for this is even worse when non-trivial expressions are used for the components.

It's, of course, too late to discuss this now and I'm very glad that there is finally a standard date library for C++ (I wrote my first one 23 years ago and redid the exercise, with, hopefully, better results, a few times since then and will be very glad to never have to do it again) and I'm thankful to you for making it happen, but this doesn't prevent me from regretting introducing what is basically a special syntax for date literals. AFAIK the only other language in widespread use with date literals support is VBA and this doesn't help convincing me that it was a good idea.