[SERVER-26906] Duration's implicit converting constructor should be constexpr Created: 04/Nov/16  Updated: 04/Jun/19  Resolved: 31/May/19

Status: Closed
Project: Core Server
Component/s: Internal Code
Affects Version/s: None
Fix Version/s: 4.1.14

Type: Improvement Priority: Major - P3
Reporter: Mira Carey Assignee: A. Jesse Jiryu Davis
Resolution: Done Votes: 0
Labels: platforms-re-triaged
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Sprint: Dev Tools 2019-06-03
Participants:

 Description   

While duration's basic constructor is constexpr (as is it's default) allowing for:

constexpr Seconds foo(20);

It's implicit converting construct is not. Disallowing:

constexpr Milliseconds foo = Seconds(20);

This reduces the readability of Duration's (compared to standard durations) when constexpr semantics are required.

I think that in the past we avoided this because it requires a constexpr duration_cast, which is painful to write in C++11. Extended constexpr in C++14 should simplify things though, so I'd like to revisit as part of the C++14 codebase epic.



 Comments   
Comment by Githook User [ 31/May/19 ]

Author:

{'name': 'A. Jesse Jiryu Davis', 'email': 'jesse@mongodb.com', 'username': 'ajdavis'}

Message: SERVER-26906 Add constexpr function to convert Durations
Branch: master
https://github.com/mongodb/mongo/commit/a587fc4fe1e0c051b8f534d6c2efad35314a899c

Comment by Mark Benvenuto [ 22/Dec/16 ]

I misread the cppreference website. It prohibits variables without intializers.

I also forgot there is another reason why it cannot be done as is. VS 2015 does not support extended constexpr.

https://msdn.microsoft.com/en-us/library/hh567368.aspx

Comment by Andrew Morrow (Inactive) [ 22/Dec/16 ]

Local variables are allowed in C++14 extended constexpr. Or is that MSVC hasn't implemented?

Comment by Mark Benvenuto [ 21/Dec/16 ]

The implicit constructor depends on duration_cast which cannot be made constexpr. It depends on mongoSignedMultiplyOverflow64 which calls into non-constexpr code. Also, current it declares local variables which are also not allowed.

Comment by Andy Schwerin [ 04/Nov/16 ]

That's right. One of the duration_cast directions can't be written constexpr in C++11 because it throws an exception in some paths (when the conversion would cause overflow, IIRC).

Generated at Thu Feb 08 04:13:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.