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

Darin Adler darin at apple.com
Tue Jun 1 20:54:34 PDT 2021

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

More information about the webkit-dev mailing list