4 ms·
> 2. One can handle errors in initialization without exceptions and half-initialized objects: Make your constructor private and expose a static member function
by hoistbypetard 7y ago
> 2. One can handle errors in initialization without exceptions and half-initialized objects: Make your constructor private and expose a static member function that returns an optional<T> (where T is the given class).
This comment criticizes an article written in 2012 about a project developed starting in 2007 for not using a technique that debuted in the 2017 edition of ISO C++. For projects that could force all consumers to use C++11 or later, it'd be straightforward to add your own.
The idea of doing so in a cross-compiler way when you need to support the original ISO standard or C++0x in the late 2000s sounds very problematic.
- ChrisLomont 7y agoIt's not hard to write an optional<T> in C++ 2003, and likely can be done in C++ 98. If you don't like that, return a T*, null if not created correctly.
- hoistbypetard 7y agoI didn't mean to suggest the language spec would have made it hard so much as that I'd have really not wanted to approach it in a way that was going to work with VC6/2002/2005/gcc 2.95/sunpro/ibm/hp compilers of the era. (I think it was 2009 before I got to push VC6 off the raft.) ISO support in C++ compilers was really bad for a long time. Our typical pattern was to return T*. But that fits well with the "I should have just written it in C" argument IMO.
- deleted 7y ago[deleted]