[libcxx-commits] [libcxx] [libc++] Cooperation between `std::optional` and other standard types (PR #93672)

Mital Ashok via libcxx-commits libcxx-commits at lists.llvm.org
Wed May 29 05:31:24 PDT 2024


MitalAshok wrote:

TODO:
 * Add more tests
 * Bikeshed more appropriate name

Cons:
 * When `__builtin_is_within_lifetime` isn't available (which neither Clang nor GCC currently provide), this is technically non-conforming:
    ```c++
    extern int i;
    constexpr bool f() {
        std::optional<std::reference_wrapper<int>> opt(i);
        std::reference_wrapper<int>& r = *opt;
        opt.reset();
        return &r.get() == &i;
    }
    static_assert(f());  // This will always pass if `!__has_builtin(__builtin_is_within_lifetime)`
    ```

    however, this already does pass with libc++: <https://godbolt.org/z/Yo5jf9cov>, and the cost to avoid this issue would cause `optional<T>` to be ABI incompatible between compilers depending on if they have `__builtin_is_within_lifetime`
 * This is a lot of added complexity in terms of tests / edge cases. The implementation for further classes should be straightforward enough (see `__functional/reference_wrapper.h`), but each of those would need to be tested.

Possible future direction:
 * Support standard containers / `std::basic_string` where the allocator is trivially destructible and trivially default constructible
 * Support `std::function`

These are based on the usages of `std::optional` in Clang.

https://github.com/llvm/llvm-project/pull/93672


More information about the libcxx-commits mailing list