[PATCH] libstdc++: Disallow duration of cv-qualified types and references.

Jonathan Wakely jwakely@redhat.com
Fri Apr 24 15:03:07 GMT 2026


On Fri, 24 Apr 2026 at 15:59, Tomasz Kamiński <tkaminsk@redhat.com> wrote:
>
> This implements LWG 4481, "Disallow chrono::duration<const T, P>",
> which was approved in Croydon 2026.
>
> libstdc++-v3/ChangeLog:
>
>         * include/bits/chrono.h: Add static_assert requiring cv-unqualified
>         non-reference type.
>         * testsuite/20_util/duration/io.cc: Remove test for duration
>         with const rep.
>         * testsuite/20_util/duration/requirements/typedefs_neg4.cc:
>         New test.
> ---
> Testing on x86_64-linux. All *duration* tests passed in all standard modes.
> OK for trunk when all test passes?
>
>  libstdc++-v3/include/bits/chrono.h                 |  4 ++++
>  libstdc++-v3/testsuite/20_util/duration/io.cc      | 14 --------------
>  .../20_util/duration/requirements/typedefs_neg4.cc | 11 +++++++++++
>  3 files changed, 15 insertions(+), 14 deletions(-)
>  create mode 100644 libstdc++-v3/testsuite/20_util/duration/requirements/typedefs_neg4.cc
>
> diff --git a/libstdc++-v3/include/bits/chrono.h b/libstdc++-v3/include/bits/chrono.h
> index fd91505c16f..d639cfaa485 100644
> --- a/libstdc++-v3/include/bits/chrono.h
> +++ b/libstdc++-v3/include/bits/chrono.h
> @@ -514,6 +514,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>      template<typename _Rep, typename _Period>
>        class duration
>        {
> +       // _GLIBCXX_RESOLVE_LIB_DEFECTS
> +       // 4481. Disallow chrono::duration<const T, P>
> +       static_assert(is_same<_Rep, __remove_cvref_t<_Rep>>::value,
> +                     "rep should be cv-unqualified object type");
>         static_assert(!__is_duration<_Rep>::value,
>                       "rep cannot be a std::chrono::duration");
>         static_assert(__is_ratio<_Period>::value,
> diff --git a/libstdc++-v3/testsuite/20_util/duration/io.cc b/libstdc++-v3/testsuite/20_util/duration/io.cc
> index 6ada4fcfa84..463d9feb21f 100644
> --- a/libstdc++-v3/testsuite/20_util/duration/io.cc
> +++ b/libstdc++-v3/testsuite/20_util/duration/io.cc
> @@ -38,13 +38,6 @@ test01()
>    ss << duration<char16_t>(126) << ' ';
>    ss << duration<char32_t>(127) << ' ';
>    VERIFY( ss.str() == "121s 122s 123s 124s 125s 126s 127s " );
> -
> -  ss.str("");
> -  ss << std::hex << std::uppercase << duration<const char>(0x1A) << ' ';
> -  ss << std::hex << std::uppercase << duration<const wchar_t>(0x2A) << ' ';
> -  ss << std::hex << std::uppercase << duration<signed char>(0x3A) << ' ';

This line isn't a cv-qualified type. Is it redundant with other checks above?
We could just remove the 'const' from these lines.

> -  ss << std::scientific << duration<const double>(4.5) << ' ';
> -  VERIFY( ss.str() == "1As 2As 3As 4.500000E+00s " );
>  }
>
>  void
> @@ -77,13 +70,6 @@ test02()
>    ss << duration<char16_t>(126) << ' ';
>    ss << duration<char32_t>(127) << ' ';
>    VERIFY( ss.str() == L"121s 122s 123s 124s 125s 126s 127s " );
> -
> -  ss.str(L"");
> -  ss << std::hex << std::uppercase << duration<const char>(0x1A) << ' ';
> -  ss << std::hex << std::uppercase << duration<const wchar_t>(0x2A) << ' ';
> -  ss << std::hex << std::uppercase << duration<signed char>(0x3A) << ' ';
> -  ss << std::scientific << duration<const double>(4.5) << ' ';
> -  VERIFY( ss.str() == L"1As 2As 3As 4.500000E+00s " );
>  #endif
>  }
>
> diff --git a/libstdc++-v3/testsuite/20_util/duration/requirements/typedefs_neg4.cc b/libstdc++-v3/testsuite/20_util/duration/requirements/typedefs_neg4.cc
> new file mode 100644
> index 00000000000..52a5d92cc8b
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/20_util/duration/requirements/typedefs_neg4.cc
> @@ -0,0 +1,11 @@
> +// { dg-do compile { target c++11 } }
> +
> +#include <chrono>
> +
> +const int i = 10;
> +// Check if rep is a cv-unqualifed object type
> +std::chrono::duration<const int> d1(i); // { dg-error "required from here" }
> +std::chrono::duration<volatile int> d2(i); // { dg-error "required from here" }
> +std::chrono::duration<const int&> d3(i); // { dg-error "required from here" }
> +
> +// { dg-error "rep should be cv-unqualified object type" "" { target *-*-* } 0 }
> --
> 2.53.0
>



More information about the Libstdc++ mailing list