Live data from Hacker News

Hypothetical C++: easy type creation

spiria.com

11–12 of 12 posts

Re: Hypothetical C++: easy type creation

#11

These are called "strong/opaque typedefs". There have been multiple proposals in the past to add them to C++ over the years (N1706, N1891, N3515, N3741, P0109); the main roadblock is that most people would like to allow to edit a type's interface after creating the new type (for example, by preventing the sum of two int-like ProgramVersions), which is for example non-trivial for primitive types. It should also of cou…

Also called "newtype".

Re: Hypothetical C++: easy type creation

#12
post #3

Good discussion and I would love more lightweight type creation in C++. The "Allow internal functionality" proposal doesn't fully work as-is. Imagine you have a type Scalar, which has a function: Scalar Scalar::scale(Scalar); If you now do typedecl Scalar Celcius; you'd get a function for it with the following signature: Celcius Celcius::scale(Celcius); However, the function you'd actually want is Celcius Celcius::sc…

This is discussed in the section about including external functionality through a mechanism of guided mapping. In the proposed syntax, that would be done like this:

    typedecl Celsius clone Scalar Scalar::scale(Scalar) -> Celsius Celsius::scale(Scalar);
(In the blog post, the example given is with std::pow())
Post reply on HN