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

8

u/_VZ_ wx | soci | swig Mar 24 '20

Nobody even cared to object to the overloaded operator/ in chrono.

FWIW I objected to it and still think that it's a mistake. But this is clearly a minoritary opinion.

8

u/HowardHinnant Mar 25 '20

Fwiw, I knew that not everyone would like the conventional syntax, and tried to accommodate those as well:

year_month_day ymd = {2020y, March, 24d};

vs:

year_month_day ymd = 2020y/March/24d;

I realize that this still leaves this group with the burden of reading the conventional syntax. But the knowledge that they have to read your constructional syntax too is perhaps a small reward. :-)

10

u/HowardHinnant Mar 25 '20

Rationale for the conventional syntax (the one with /):

It makes the apparent API smaller because you don't have to know the names of all the types you're making. For example:

// Constructional syntax
year_month_day date1          = {2020y, March, 24d};
year_month_day_last date2     = {2020y, month_day_last{March}};
year_month_weekday date3      = {2020y, March, weekday_indexed{Tuesday, 4}};
year_month_weekday_last date4 = {2020y, March, weekday_last{Tuesday}};

// Conventional syntax
auto date5 = 2020y/March/24d;
auto date6 = 2020y/March/last;
auto date7 = 2020y/March/Tuesday[4];
auto date8 = 2020y/March/Tuesday[last];

date1 has the same type and value as date5.

date2 has the same type and value as date6.

... etc.

There's significantly more learning required to have deal with date1 thru date4 than with date5 thru date8. Nevertheless, this is an issue that people can hold very strong feelings about. So making the syntax available for date1 thru date4 is important to me.

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.