It has been one year since my previous post: Initialization in C++: A corner case. In this post, I would like to share some corner cases (with implementation divergences) encountered while testing std::optional
's constructors. To maximize flexibility and usability, std::optional
provides a rich set of overloaded constructors, along with some heavy SFINAE logic to disambiguate them.1 The downside is that certain forms of initialization may behave unexpectedly. In the following sections, I will present several examples and explain how they are interpreted by various implementations and by the C++ standard. All references to the standard are to N4950 (the C++23 final draft).
First, let's write down the re
Footnotes
-
This topic is discussed in detail in Barry's post: Getting in trouble with mixed construction. ↩