C++Now 2021 has ended
Back To Schedule
Thursday, May 6 • 11:30am - 1:00pm
Library Approaches for Strong Type Aliases

Log in to save this to your schedule, view media, leave feedback and see who's attending!

It is tempting when writing function and class interfaces to use built-in or library types as function parameters and return values. "I want something to represent a count, I'll use an int", "I want something to represent a name, I'll use a std::string", etc. This works fine up to a point, but allows for confusion: if I pass two ints as the parameters to a std::vector<int> constructor, which is the size and which is the value? Is this float value for "timeout" a value in seconds or milliseconds?

One way to solve this issue is with use-specific classes: std::duration specifies time period with the units encoded, and many SI units libraries abound. However, writing such classes can be tedious and domain specific. To this end, many people advocate the idea of "strong typedefs": the ability to say "this type is like this other type, but is distinct from it", so I can have a "count" type that is essentially an "int", but is distinct from a "timeout_in_seconds" type that is also an int.

I will present the library approach I developed to address this problem, which uses templates to allow you to create specific types for specific purposes, with a set of "properties" that define the interface.

avatar for Anthony Williams

Anthony Williams

Just Software Solutions Ltd
Anthony Williams is the author of C++ Concurrency in Action.

Thursday May 6, 2021 11:30am - 1:00pm MDT