libstdc++: potential bug in specification of layouts.

Luc Grosheintz luc.grosheintz@gmail.com
Thu Jun 26 07:38:21 GMT 2025



On 6/25/25 08:44, Tomasz Kaminski wrote:
>>
>> The same problem occurs when using a free standing function as
>> proposed by Tomasz. The ambiguity can be created by writing a
>> free function:
>>
>>     template<typename SMapping, typename OMapping>
>>       requires (std::__mdspan::__mapping_of<custom_layout, SMapping>
>>              || std::__mdspan::__mapping_of<custom_layout, OMapping>)
>>       constexpr bool
>>       operator==(const SMapping& self, const OMapping& other)
>>
> Yes, indeed. However, there is no reason to write such mapping as
> free-function,
> when comparing mappings belonging to specific layouts, as we will do for
> layout_left,
> layout_rigth - these comparisons can be more optimal. And with a free
> function, there is
> no reason for the user to write any cross strided-layout comparisons. This
> is why I prefer
> free-function, as it does not cause ambiguity when using member-functions.
> 

I fully agree, there's no need for users to make the overload
ambiguous in this way. What I wanted to say is that the only
way I could make the member function approach ambiguous was by
this reversing of the order (and that a really determined user
could do the same for the free standing approach).

We might also want to be careful about using __mapping_alike,
because it's:

   a) not strong enough to guarantee that the mapping has all
   the methods required to implement, e.g. they're not required
   to have an operator() or stride method.

   b) mdspan satisfies __mapping_alike [1]; but clang implements
   a different condition [2][3].

The latter implies that there's an overload of operator==
for layout_stride::mapping and mdspan; which if instantiated
causes a compiler error in our implementation.

[1]: https://eel.is/c++draft/mdspan.layout#stride.expo-4
[2]: https://godbolt.org/z/Ghd7s8ErW
[3]: CLang seems to also checks __is_mapping_of as part of
      __layout_mapping_alike. The heavily edited error message
      for the Godbolt snippet is:

layout_stride.h: note: candidate ignored: constraints not satisfied
   343 |  bool operator==(const mapping&, const _OtherMapping&) {
       |                                               ^
layout_stride.h: note: because 'mdspan' does not satisfy
                  '__layout_mapping_alike'
   341 |     requires(__layout_mapping_alike<_OtherMapping> &&
       |              ^
layout_stride.h: note: because '__is_mapping_of<mdspan::layout_type,
                        mdspan>' evaluated to false
    56 |   requires __is_mapping_of<_Mapping::layout_type, _Mapping>;
       |            ^


More information about the Libstdc++ mailing list