Narrow Contracts and noexcept are Inherently Incompatible

John Lakos

⏱ 90 minute session
14:00-15:30, Wednesday, 17th April 2024
A contract is a plain-language specification of whatever essential behavior a given function promises to deliver when invoked in contract. A function that has at least one syntactically valid combination of state and input for which the behavior is undefined has a precondition and is therefore said to have a narrow contract. The Lakos Rule effectively prohibits placing the noexcept specifier (introduced in C++11) on any function that would otherwise have a narrow contract.

This talk begins with a reprise of contracts, essential behavior, and preconditions. We’ll then go on to contrast two classic software design principles, Design by Contract and Liskov Substitutability, and then use the latter to explain how both backward compatibility and wide implementations benefit from scrupulously adhering to The Lakos Rule. We conclude that best practice is to follow this rule, especially in the specification of the C++ Standard Library, and we close with a welcome solution that satisfies essentially all needs and wants of the eclectic C++ multiverse.

🏷 Contracts
🏷 Software Engineering
🏷 Principled Design
🏷 Undefined Behavior
🏷 Backward Compatibility
🏷 Standard Library

John Lakos

John Lakos, author of Large-Scale C++ Software Design (Pearson, 1997), serves at Bloomberg LP in New York City as a senior architect and mentor for C++ Software Development worldwide. He is also an active voting member of the C++ Standards Committee’s Evolution Working Group. Previously, Dr. Lakos directed the design and development of infrastructure libraries for proprietary analytical financial applications at Bear Stearns. For 12 years prior, Dr. Lakos developed large frameworks and advanced ICCAD applications at Mentor Graphics, for which he holds multiple software patents. His academic credentials include a Ph.D. in Computer Science ('97) and an Sc.D. in Electrical Engineering ('89) from Columbia University. Dr. Lakos received his undergraduate degrees from MIT in Mathematics ('82) and Computer Science ('81). He is the author of the multi-volume book Large-Scale C++, the first volume of which, Volume I: Process and Architecture (Pearson, 2020), is currently available, and subsequent volumes are forthcoming. He is the coauthor of Embracing Modern C++ Safely (Pearson, 2021) along with Vittorio Romeo, Rostislav Khlebnikov, and Alisdair Meredith.