STL icon indicating copy to clipboard operation
STL copied to clipboard

`<mdspan>`: `std::mdspan` should reject reference `ElementType`

Open eiytoq opened this issue 7 months ago • 1 comments

Describe the bug

std::mdspan accepts a reference type as its ElementType.

Reproduction

#include <mdspan>

struct BadAccessor {
	using offset_policy = BadAccessor;
	using element_type = int&;
	using reference = int&;
	using data_handle_type = int*;

	reference access(data_handle_type p, std::size_t i) const {
		return p[i];
	}
};

int main() {
	std::mdspan<int&, std::dextents<int, 2>, std::layout_right, BadAccessor> m;
}

Expected behavior

According to [mdspan.mdspan.overview]

ElementType is a complete object type that is neither an abstract class type nor an array type,

The program should be rejected as ill-formed.

STL version

202508

Additional context

libstdc++ and libc++ also accept this. https://godbolt.org/z/PeP6beWWb

eiytoq avatar Apr 12 '26 13:04 eiytoq

Confirmed. This wording has been present since the beginning, and https://github.com/microsoft/STL/blob/da06be8fa38418772506328177b703b28ca75699/stl/inc/mdspan#L1257-L1267 simply didn't enforce it.

StephanTLavavej avatar Apr 13 '26 05:04 StephanTLavavej