Partially-Formed Objects For Fun And Profit

Speaker: Marc Mutz

Audience level: Beginner | Intermediate | Advanced

Lately, partially-formed states have gained some traction as the natural state of moved-from objects. Alexander Stepanov, in his landmark book Elements of Programming (EoP), defines them as a state in which the only valid operations are assignment and destruction. This state was, and continues to be, essential to high-performance code, and to the C++ guarantee of "don't pay for what you don't use".

After a brief introduction, we will look at where in the C/C++ language such states appeared even before there was a "C++", or the name "partially-formed", and certainly before the discussion about the moved-from state started. We thereby show that whatever you may think about the partially-formed state concept, it's there, as a Platonic Ideal in the world of computer programming. We will attempt nothing less than to carry the EoP treatment further into modern C++. In keeping with the spirit of EoP, there will be Propositions. We shall find that move semantics, as currently defined in the standard, are too strong, violating "don't pay for what you don't use", and give an overview of weaker alternatives, which are just as easy to learn and reason about, yet allow the most natural and efficient implementation of types that use them. A prime example will be flat_map.

We will also show how developers that feel uneasy about the partially-formed state can avoid them at little to no cost, neither in code readability, nor performance, and use these examples to propose a new (or old) paradigm for API design: safe and unsafe functions (in the Sean Parent sense).