[Development] Improving toInt, toLong, etc.

Matthew Woehlke mw_triad at users.sourceforge.net
Wed Feb 5 20:28:36 CET 2014


On 2014-02-05 13:04, Olivier Goffart wrote:
> On Wednesday 05 February 2014 09:44:39 Thiago Macieira wrote:
>> Em qua 05 fev 2014, às 12:36:42, Matthew Woehlke escreveu:
>>> On 2014-02-05 12:19, Thiago Macieira wrote:
>>>> We need to return something.
>>>
>>> ...or you could abort, which I assume is what operator* does?
>>
>> That doesn't help. [...] we still must return something.
>
> You will just have an undefined behaviour.  You will return the content of the
> uninitialized memory cast to the type, and it will likely cause a crash later.
> Debug build will abort,  release build will probably crash somehow too.
> Same as QList::at()
>
> // possible implementation
> T &QOptional<T>::operator*() {
>     Q_ASSERT(isValid());
>     return reinterpret_cast<T&>(data);
> };

Right. I don't see why this seems to be a problem here, but is not for 
e.g. an invalid iterator, or at() / operator[] on a container with an 
invalid index. Just do what those do.

-- 
Matthew




More information about the Development mailing list