On Fri, 24 Apr 2026 at 16:25, Tomasz Kamiński <[email protected]> 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 const-qualifier in
>         stream manipluators tests.
>         * testsuite/20_util/duration/requirements/typedefs_neg4.cc:
>         New test.
> ---
> v2 removes const from manipulators test instead of nuking them completly.
>
> Testing on x86_64-linux. *duration* test passed again.
> OK for trunk when all tests passes?

OK, thanks

>
>  libstdc++-v3/include/bits/chrono.h                   |  4 ++++
>  libstdc++-v3/testsuite/20_util/duration/io.cc        | 12 ++++++------
>  .../20_util/duration/requirements/typedefs_neg4.cc   | 11 +++++++++++
>  3 files changed, 21 insertions(+), 6 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..83ecd33c038 100644
> --- a/libstdc++-v3/testsuite/20_util/duration/io.cc
> +++ b/libstdc++-v3/testsuite/20_util/duration/io.cc
> @@ -40,10 +40,10 @@ test01()
>    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<char>(0x1A) << ' ';
> +  ss << std::hex << std::uppercase << duration<wchar_t>(0x2A) << ' ';
>    ss << std::hex << std::uppercase << duration<signed char>(0x3A) << ' ';
> -  ss << std::scientific << duration<const double>(4.5) << ' ';
> +  ss << std::scientific << duration<double>(4.5) << ' ';
>    VERIFY( ss.str() == "1As 2As 3As 4.500000E+00s " );
>  }
>
> @@ -79,10 +79,10 @@ test02()
>    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<char>(0x1A) << ' ';
> +  ss << std::hex << std::uppercase << duration<wchar_t>(0x2A) << ' ';
>    ss << std::hex << std::uppercase << duration<signed char>(0x3A) << ' ';
> -  ss << std::scientific << duration<const double>(4.5) << ' ';
> +  ss << std::scientific << duration<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
>

Reply via email to