C++ Logo

std-discussion

Advanced search

Implementing std::optional<T>::transform

From: Rasheeq Azad <rasheeqhere_at_[hidden]>
Date: Sun, 14 Dec 2025 16:00:00 -0500
In the current draft, [optional.monadic] specifies that
std::optional<T>::transform(F &&f) & shall do the following:

> Let U be remove_cv_t<invoke_result_t<F, decltype(*val)>>.
> Mandates: ...
> [Note 1: There is no requirement that U is movable ([dcl.init.general]).
> Returns: If *this contains a value, an optional<U> object whose contained value is direct-non-list-initialized with invoke(std::forward<F>(f), *val); otherwise, optional<U>().

I'm wondering how this is supposed to be implemented. None of the
standard constructors of std::optional<U> directly support
initializing the contained object with an invocable. Both GCC
libstdc++ [1a] [1b] [1c] and LLVM libc++ [2a] [2b] [2c] get around
this by adding an internal-use-only (using a tag type that has a
reserved name) constructor to std::optional<T> that takes the
invocable and calls it to initialize the contained object. But this
means that std::optional<T> relies on the *different specialization*
std::optional<U> supporting this nonstandard constructor.

As a consequence, neither library can fully cope with me defining my
own specialization of std::optional. I hoped this was just an
implementation bug and filed an issue with LLVM about it, [3], which
contains a code example and my proposed solution (see also on
[Godbolt]), but someone else pointed out that my proposed solution
also has an edge case. So as far as I can tell right now, it seems
impossible to implement std::optional<T>::transform while using only
the standard interface of the other specialization std::optional<U>.

If this really is the case, then the standard seems to have
inadvertently prohibited program-defined specializations of
std::optional with the addition of std::optional<T>::transform. I'm
hoping to get more eyes on this question. If someone in this list
could come up with a way to implement std::optional<T>::transform in a
way that permits program-defined specializations (or say with more
certainty that it's in fact not possible), that'd be great. Have I
found a standard defect?

- Rasheeq Azad

[1a]: https://github.com/gcc-mirror/gcc/blob/ffa98429b93934be87f58bc3af481a69310aedaf/libstdc%2B%2B-v3/include/std/optional#L97
[1b]: https://github.com/gcc-mirror/gcc/blob/ffa98429b93934be87f58bc3af481a69310aedaf/libstdc%2B%2B-v3/include/std/optional#L1490-L1493
[1c]: https://github.com/gcc-mirror/gcc/blob/ffa98429b93934be87f58bc3af481a69310aedaf/libstdc%2B%2B-v3/include/std/optional#L1391
[2a]: https://github.com/llvm/llvm-project/blob/9975cb166ea3925738abb8d0db0028e418315eda/libcxx/include/optional#L369
[2b]: https://github.com/llvm/llvm-project/blob/9975cb166ea3925738abb8d0db0028e418315eda/libcxx/include/optional#L925-L927
[2c]: https://github.com/llvm/llvm-project/blob/9975cb166ea3925738abb8d0db0028e418315eda/libcxx/include/optional#L1144
[3]: https://github.com/llvm/llvm-project/issues/172197
[Godbolt]: https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYgATKVpMGoZAFI9AIQuXSS%2BsgJ4BlRugDCqWgFcWDCaRuADJ4DJgAcr4ARpjEIACs8aQADqgKhM4Mnj5%2BAanpTgIhYZEsMXGJdpgOhQxCBEzEBNm%2B/lxVNZn1jQTFEdGxCUkKDU0tue0jPX2l5UMAlHao3sTI7BwWAMyhyD5YANRmm%2B78xCxMBEfYZhoAgls7e5iHx84jxJisVzf3etsMu28ByO7neoWA3zuPweAKeL08yVqYkh9zuuyYCgU%2BxYAE8APpRVBeQ4Adks%2BwA9BT9kx9sliKhgMRWABaLA0MLofYEHHJZ4ASWxeGACAI%2BwA7sYxUR9go%2Bcg8GI8AAvZ4jdAgECoRGZMT7U4/fb7ZAIRr7VRHax3ZLeKK0PDIECG/aYVTJe0KsW4glE2gQQnE1TzfYgc0QIOk8lmEkAEWd3oDfoTvosADY08GjjH9lh6ARMJbnQA/ZNeCCZsmkuObK2o25G13uh2Efba2IXEj7RPl40CEaR/YfAgrBjmy1V%2BP4xOtvnMojEMCQYNoBj96Pkocj7FT30QRfdoPzcfRuNQ2OFqF3fMsd0XAvHK7GoyY2UEDVanUCZHHUu0R/r513m8RwaSiJRBDxMV12NU1iFlRgnDCWgXmzPRj3PGtnW8BhMgHX86Q%2BcDLhrUCiMg0iEPQ6tySpWUmAAN0wLlaSiHF80OAAxDQzAATg4sxbniMxLE2R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