on-internet
← Community

should I avoid std::optional, similarly to std::vector?

Stack Overflow ·

const in C++ is not so simple to use and reason about when it comes to composite types. Many realize this fact when considering std::vector: non-const vectors of const elements vs. const vector of non-const elements: How can I specify a vector parameter so that values may change but the vector structure won't? const vector implies const elements? Now, what about std::optional? If we use a std::optional<const T>, we can clear or replace the value of T - without changing the existing value, as constructing and destructing const T is quite legitimate. So, the const'ness is somewhat arbitrary. Sho

Comments

Sign in or sign up to comment and upvote.

No comments yet.