Loading…
C++Now 2019 has ended
Monday, May 6 • 14:30 - 16:00
How I learned to Stop Worrying and Love the C++ Type System

Log in to save this to your schedule and see who's attending!

Feedback form is now closed.
Ward Cunningham's CHECKS pattern language of the 1990s contains the Whole Value Pattern. That pattern claims that using plain value types such as integers, strings or double, especially on a function's interface are not a good practice, because their use is error prone. There are stories about programmers calling a function taking several ints with the wrong sequence of numbers and searching for the bugs. Some claim that named parameters solve those problem, but in my opinion they don't.

C++ has the huge benefit to allow us to define own encapsulated types that are implemented as efficient as the built-in types and that can be clearly distinguished from each other. Unfortunately due to C++'s legacy there are some holes in the type system, like integral promotion and numeric conversions or type aliases, that add to the problem of using plain types for domain values. We should change that practice.

What is a domain value? Even the standard library suffers from using type aliases where a proper domain type would have lessened the issues. The recent discussion of std::span's index type (int or unsigned) shows that. If the standard library would have defined a proper index_type and size_type that are not just integers, that discussion would have been moot and the desired and needed operators for these types could have been defined in a way avoiding the pitfalls of while(v.size()-1 >=0).

This talks demonstrates how to implement and use simple wrapper types and how we could teach that. It also introduces a work-in-progress framework supporting mixing in the minimum set of operators in a type safe way, so that problems of integral promotion or numeric conversion can not happen. Feedback on the latter is appreciated. It deliberately does not attempt to be a full units framework, but to allow distinguish things like width, height and radius from each other and from 42.

Speakers
avatar for Peter Sommerlad

Peter Sommerlad

Professor, IFS Institute for Software
Cevelop, Safe C++, CUTE unit testing


Monday May 6, 2019 14:30 - 16:00
Hudson Commons

Attendees (37)