CppCoreGuidelines icon indicating copy to clipboard operation
CppCoreGuidelines copied to clipboard

gsl::not_null conversion operator

Open JordanMaples opened this issue 4 years ago • 3 comments

An issue raised on the Microsoft/GSL that requests a conversion operator on gsl::not_null for non-copy constructible types (Microsoft/GSL#991).

Would the Guidelines editors like to see something like this added for gsl::not_null?

JordanMaples avatar Sep 15 '21 23:09 JordanMaples

This is consistent with what we've done for the base() members of iterator adaptors in C++20, to support move-only iterators.

Should the const& one really return a reference though? My gut feeling is that for this type both should return by value, although I haven't given it much thought.

jwakely avatar Sep 16 '21 09:09 jwakely

Editors call: Would you please write up some pros and cons?

hsutter avatar Sep 30 '21 18:09 hsutter

This issue had come up in gsl-lite as well, and we decided to always return a value type from implicit conversion operators. The reasons are discussed in the comment here:

https://github.com/gsl-lite/gsl-lite/blob/a8c7e5bbbd08841836f9b92d72747fb8769dbec4/include/gsl/gsl-lite.hpp#L3007-L3034

mbeutel avatar Aug 12 '23 20:08 mbeutel