Common way to indicate non-legal value for many types without UB

Viewed 81

I'm writing some kind of (open-addressing) hash table.

For all valid types T that I want to allow to be contained in my hash table, I want some non-legal value for that type to indicate that the table slot is empty.

The simplest solution I know is:

  1. Let the table be std::vector<std::optional<T>>
  2. Set that non-legal value as std::nullopt<T>

But I'm scared of possible overhead (because all entries become std::optional in this way). Is there any faster and at least equally safe way?

For T, I set the restriction as: std::is_same_v<std::remove_cvref_t<T>, T>. Other than this there is no restriction.

0 Answers
Related