Relax Requirements for time_point::clock

Published Proposal,

This version:
Latest published version:
ISO/IEC JTC1/SC22/WG21 14882: Programming Language — C++

1. Abstract

We propose to relax the requirements on the Clock template parameter of std::chrono::time_point.

2. Revision History

2.1. Revision 2

Add proposed wording for [thread.req.paramname] per Tim Song’s suggestion.

Rebase proposed wording to the current working draft.

2.2. Revision 1

Alter proposed wording per Jonathan Wakely’s suggestion.

2.3. Revision 0

Initial revision.

3. Motivation

It is sometimes useful to give time_point a Clock template argument that does not meet the Cpp17Clock requirements. The most obvious use case is the local_t introduced in C++20. This "clock" is not really a clock at all. It has no functionality for getting the current time. Nevertheless, C++20 utilizes time_point<local_t, Duration> to represent a family of time points that do not represent instants in time until they are paired with a specific time_zone. This gives "local time" a distinct type from "system/UTC time" in order to reduce the possibility of the programmer confusing these two types in their code.

Since the introduction of local_t, more use cases for "not-quite-clocks" have become apparent:

4. Wording

Remove paragraph 1 in [time.point.general]:

Clock shall either meet the Cpp17Clock requirements ([time.clock.req]) or be the type local_t.

Edit the last sentence of paragraph 1 in [thread.req.paramname] as indicated:

If a template parameter is named Clock, the corresponding template argument shall be a type C that meets the Cpp17Clock requirements ([time.clock.req]); the program is ill-formed if is_clock_v<C> is false. for which is_clock_v<C> is true; otherwise the program is ill-formed.

5. Effect on existing code

This change should not affect any conforming code because the requirement is only relaxed.

6. Effect on implementations

This change should not have an effect on most of implementations because in practice they do not use any of the members of Clock outside of [thread]. [thread.req.paramname] places additional requirements on the Clock template parameter, namely that is_clock_v<Clock> is true. This change is effectively already proven to be safe because of the existence of std::chrono::time_point<std::chrono::local_t, Duration>, where std::chrono::local_t is an empty class.

7. Acknowledgement

Thanks to all those who provided the motivating use cases for this change.