Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default sentinals for more types? #1

Closed
aardappel opened this issue Nov 8, 2022 · 3 comments
Closed

Default sentinals for more types? #1

aardappel opened this issue Nov 8, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@aardappel
Copy link

Love the concept of this library, but to me it would be more useful it would pretty much guarantee to have a sentinel for every T as opposed to use an extra bool fallback for some types. I'd rather it would error if a sentinel wasn't possible.

In particular, default to INT_MIN and similar for all signed integer types, and MAX_UINT and similar for all unsigned types. Obviously there's times when these values don't work but it will work for almost everything and means you can directly use optional<T> instead of needing a manual case for optional<int, INT_MIN> or whatever. -1 is used too often as an actual valid value.

Also don't see why the sentinel for raw pointers isn't simply nullptr, the idea of having an optional value of an optional pointer. Makes these values also a bit more understandable in a debugger etc.

@Sedeniono
Copy link
Owner

Thanks for your feedback! One of the major goals of this library was to have a drop-in replacement for std::optional with the same interface (as far as possible) while getting optimized sizes automatically where possible. Hence, the type tiny::optional does not automatically "swallow" any legal values such as INT_MIN for signed integers or nullptr for pointers. Also, not all payload types can appear as sentinel values: For tiny::optional itself the sentinel must be specified via a non-type-template-parameter, and only few types are supported there. Of course, using different interfaces one can relax this (cf. tiny::optional_empty_via_type). But still there are types that simply cannot be used as sentinel (think about a missing comparison operator).

But it is certainly possible to define another type besides tiny::optional where a sentinel has to be specified if the payload type does not have unused bit patterns. I have added it to my TODO list.

@Sedeniono Sedeniono added the enhancement New feature or request label Nov 9, 2022
Sedeniono added a commit that referenced this issue Nov 19, 2022
Similar to tiny::optional, but its size is guaranteed to be equal to the payload. To this end, for integers it automatically swallows some value. If no sentinel can be selected automatically, a compilation error occurs.

This type was proposed in issue #1
Sedeniono added a commit that referenced this issue Nov 19, 2022
Similar to tiny::optional, but its size is guaranteed to be equal to the payload. To this end, for integers it automatically swallows some value. If no sentinel can be selected automatically, a compilation error occurs.

This type was proposed in issue #1
@Sedeniono
Copy link
Owner

@aardappel I implemented the new type tiny::optional_aip in 7eb6dd6 which should do what you want: Exploit unused bit patterns where possible, use default sentinels for integers, and otherwise produce a compilation error if the user does not provide a sentinel. Compare the updated readme.

@aardappel
Copy link
Author

@Sedeniono that is pretty awesome, very useful :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants