Fixing C's Biggest Mistake

Walter Bright newshound2 at digitalmars.com
Mon Jan 2 22:53:30 UTC 2023


On 12/31/2022 2:28 AM, Max Samukha wrote:
> For types that require runtime construction, initializing to 
> T.init does not result in a constructed object.
The idea is to:

1. have construction that cannot fail. This helps avoid things like double-fault 
exceptions

2. have initializers that can be placed in read only memory

3. have something to set a destroyed object to, in case of dangling references 
and other bugs

4. present to a constructor an already initialized object. This prevents the 
common C++ problem of adding a field and forgetting to construct it in one of 
the overloaded constructors, a problem that has plagued me with erratic behavior

5. provide a NaN state. I know many people don't like NaN states, but if one 
does, the default construction is perfect for implementing one.

6. it fits in well with (future) sumtypes, where the default initializer can be 
the error state.

An alternative to factory functions is to have a constructor with a dummy 
argument. Nothing says one has to actually use the parameters to a constructor.


More information about the Digitalmars-d mailing list