martinmoene / optional-lite

optional lite - A C++17-like optional, a nullable object for C++98, C++11 and later in a single-file header-only library
Boost Software License 1.0
403 stars 45 forks source link

optional as a literal type #77

Closed oliverlee closed 9 months ago

oliverlee commented 9 months ago

Hi Martin,

I've created a draft PR to allow use optional types within constant expressions. Can you let me know if this is something you would be interested in accepting and if so, is this the right direction? I have never written code for C++03 and very little for C++11 and would appreciate any guidance from you.

martinmoene commented 9 months ago

Hi Oliver (@oliverlee),

Sorry for taking so long to react.

Thanks for your effort. It is something I've been looking at on and off, mostly off... as its quite involved and I ran into issues I did not see a solution to at the time. There are of course examples out there how it can be done, at least from C++11 onwards, I think.

Please note that the choice for a union for the object's storage is to enable creating an empty optional, without requiring default constructing a T. See e.g. reference [10] Andrei Alexandrescu. Generic: Discriminated Unions part 1, part 2, part 3. April 2002.

I don't think I have the time/focus/energy to see such a change through at the moment.

Thanks again.

oliverlee commented 9 months ago

No worries. I'll close for now but feel free to ping if you find more time in the future.

Use of a union member in a struct allows for empty optional types without a default constructible T - at least for newer versions of C++. I'm not sure about C++03.

martinmoene commented 9 months ago

Thank you @oliverlee .