[PATCH v1] libstdc++: Refactor tests for mdspan related accessors.
Tomasz Kaminski
tkaminsk@redhat.com
Mon Jul 28 08:15:36 GMT 2025
On Sun, Jul 27, 2025 at 3:38 PM Luc Grosheintz <luc.grosheintz@gmail.com>
wrote:
> Versions 1, 2 and 3 of the patch for adding aligned_accessor had a
> bug in the constraints that allowed conversion of
>
> aligned_accessor<T, N> a = aligned_accessor<const T, N>{};
>
> and prevented the reverse.
>
> The file mdspan/accessors/generic.cc already contains code that checks
> all variation of the constraint. This commit allows passing in two
> different accessors. Enabling it to be reused more widely.
>
> libstdc++-v3/ChangeLog:
>
> * testsuite/23_containers/mdspan/accessors/generic.cc: Refactor
> test_ctor.
>
> Signed-off-by: Luc Grosheintz <luc.grosheintz@gmail.com>
> ---
>
LGTM, with one note about duplicated assertions. I will remove it locally,
and push
the commit.
> .../23_containers/mdspan/accessors/generic.cc | 61 ++++++++++++-------
> 1 file changed, 39 insertions(+), 22 deletions(-)
>
> diff --git
> a/libstdc++-v3/testsuite/23_containers/mdspan/accessors/generic.cc
> b/libstdc++-v3/testsuite/23_containers/mdspan/accessors/generic.cc
> index c3350353aae..70c3bd0751c 100644
> --- a/libstdc++-v3/testsuite/23_containers/mdspan/accessors/generic.cc
> +++ b/libstdc++-v3/testsuite/23_containers/mdspan/accessors/generic.cc
> @@ -29,44 +29,61 @@ class Base
> class Derived : public Base
> { };
>
> -template<template<typename T> typename Accessor>
> +template<typename RhsAccessor, typename LhsAccessor, bool
> ExpectConvertible>
> + constexpr void
> + check_convertible()
> + {
> + RhsAccessor rhs;
> + [[maybe_unused]] LhsAccessor lhs(rhs);
> + static_assert(std::is_nothrow_constructible_v<LhsAccessor,
> RhsAccessor>);
> + static_assert(std::is_convertible_v<RhsAccessor, LhsAccessor> ==
> ExpectConvertible);
> + }
> +
> +template<template<typename T> typename LhsAccessor,
> + template<typename T> typename RhsAccessor = LhsAccessor,
> + bool ExpectConvertible = true>
> constexpr bool
> test_ctor()
> {
> // T -> T
> - static_assert(std::is_nothrow_constructible_v<Accessor<double>,
> - Accessor<double>>);
> - static_assert(std::is_convertible_v<Accessor<double>,
> Accessor<double>>);
> + static_assert(std::is_nothrow_constructible_v<LhsAccessor<double>,
> + RhsAccessor<double>>);
>
This static assert seems to be redundant to one in check_convertible, so I
will remove it and the push th changes.
> + check_convertible<RhsAccessor<double>, LhsAccessor<double>,
> + ExpectConvertible>();
>
> // T -> const T
> - static_assert(std::is_convertible_v<Accessor<double>,
> - Accessor<const double>>);
> - static_assert(std::is_convertible_v<Accessor<Derived>,
> - Accessor<const Derived>>);
> + check_convertible<RhsAccessor<double>, LhsAccessor<const double>,
> + ExpectConvertible>();
> + check_convertible<RhsAccessor<Derived>, LhsAccessor<const Derived>,
> + ExpectConvertible>();
>
> // const T -> T
> - static_assert(!std::is_constructible_v<Accessor<double>,
> - Accessor<const double>>);
> - static_assert(!std::is_constructible_v<Accessor<Derived>,
> - Accessor<const Derived>>);
> + static_assert(!std::is_constructible_v<LhsAccessor<double>,
> + RhsAccessor<const double>>);
> + static_assert(!std::is_constructible_v<LhsAccessor<Derived>,
> + RhsAccessor<const Derived>>);
>
> // T <-> volatile T
> - static_assert(std::is_convertible_v<Accessor<int>, Accessor<volatile
> int>>);
> - static_assert(!std::is_constructible_v<Accessor<int>,
> - Accessor<volatile int>>);
> + check_convertible<RhsAccessor<int>, LhsAccessor<volatile int>,
> + ExpectConvertible>();
> + static_assert(!std::is_constructible_v<LhsAccessor<int>,
> + RhsAccessor<volatile int>>);
>
> // size difference
> - static_assert(!std::is_constructible_v<Accessor<char>,
> Accessor<int>>);
> + static_assert(!std::is_constructible_v<LhsAccessor<char>,
> + RhsAccessor<int>>);
>
> // signedness
> - static_assert(!std::is_constructible_v<Accessor<int>,
> - Accessor<unsigned int>>);
> - static_assert(!std::is_constructible_v<Accessor<unsigned int>,
> - Accessor<int>>);
> + static_assert(!std::is_constructible_v<LhsAccessor<int>,
> + RhsAccessor<unsigned int>>);
> + static_assert(!std::is_constructible_v<LhsAccessor<unsigned int>,
> + RhsAccessor<int>>);
>
> // Derived <-> Base
> - static_assert(!std::is_constructible_v<Accessor<Base>,
> Accessor<Derived>>);
> - static_assert(!std::is_constructible_v<Accessor<Derived>,
> Accessor<Base>>);
> + static_assert(!std::is_constructible_v<LhsAccessor<Base>,
> + RhsAccessor<Derived>>);
> + static_assert(!std::is_constructible_v<LhsAccessor<Derived>,
> + RhsAccessor<Base>>);
> return true;
> }
>
> --
> 2.50.0
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20250728/b4b1aaea/attachment-0001.htm>
More information about the Libstdc++
mailing list