[webkit-dev] Moving from WTF::Optional to std::optional

Chris Dumez cdumez at apple.com
Tue Jun 1 22:01:56 PDT 2021


Hi,

Another thing Darin didn’t mention but I think people should be careful about:
The move constructor for std::optional does not clear the is-set flag (while the one for WTF::Optional did).

As a result, you will be having a very bad time if you do a use-after-move of a std::optional. Please make sure to use std::exchange() instead of WTFMove() if you want to leave to std::optional in a clean state for reuse later.

Chris Dumez

> On Jun 1, 2021, at 8:54 PM, Darin Adler via webkit-dev <webkit-dev at lists.webkit.org> wrote:
> 
> Hi folks.
> 
> We’re getting rid of the WTF::Optional class template, because, hooray, std::optional is supported quite well by all the C++17 compilers we use, and we don’t have to keep using our own special version. Generally we don’t want to reimplement the C++ standard library when there is not a significant benefit, and this is one of those times.
> 
> Here are a few considerations:
> 
> 1) Since https://commits.webkit.org/238290@main, if you use Optional<> by mistake instead of std::optional<>, your code won’t compile. (Unless you are writing code for ANGLE, which has its own separate Optional<>.)
> 
> 2) If you want to use std::optional, include the C++ standard header, <optional>, or something that includes it. In a lot of cases, adding an include will not be required since it’s included by widely-used headers like WTFString.h and Vector.h, so if you include one of those are covered. Another way to think about this is that if your base class already uses std::optional, then you don’t need to include it.
> 
> 3) Once the patch in https://bugs.webkit.org/show_bug.cgi?id=226437 lands, includes of <wtf/Forward.h> won’t forward declare optional, and includes of <wtf/Optional.h> won’t do anything at all.
> 
> — Darin
> _______________________________________________
> webkit-dev mailing list
> webkit-dev at lists.webkit.org
> https://lists.webkit.org/mailman/listinfo/webkit-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20210601/51aad61a/attachment.htm>


More information about the webkit-dev mailing list