[Development] Improving toInt, toLong, etc.
Thiago Macieira
thiago.macieira at intel.com
Tue Feb 4 23:06:37 CET 2014
Em ter 04 fev 2014, às 16:04:11, Matthew Woehlke escreveu:
> If it were up to me, given an operator* (which is only one more
> character of typing, after all), I wouldn't provide *any* (additional)
> convenience access to the value. For roughly this reason; it's a lot of
> additional implementation effort, highly error prone, and the benefit is
> almost nil.
>
> (Which also means I would consider with QOptional making value() /
> operator*() the only way to get the interior value.)
Agreed. The implementation of operator== for optionals should only compare
against another optional. But even that needs magic due to the implicit
constructor.
template<typename X, typename Y> bool
operator==(const optional<X> &l, const optional<Y> &r)
{ return !l ? !r : r && *l == *r; }
Then this still matches:
if (optional<bool>() == false)
For that matter, this also compiles:
bool *ptr = 0;
if (ptr == false)
although some compilers might print warnings.
> Loosely related: I guess if the default behavior of value() on a
> disengaged optional is to silently construct a default, then the full
> signature should be:
>
> T value(T&& default = {}) const;
You probably mean
const T &value(T &&invalidAs = {}) const;
But I'm not sure I'm comfortable with requiring rvalue refs for this
particular piece of code. It would make the entire QOptional C++11 only and,
thus, the entire new functionality in QString, QStringRef, QByteArray, QLocale
and force kdbus support to also require C++11. It's not a step I'm ready to
take just yet.
> However, now that I think about it, I still somewhat feel like value()
> should abort if disengaged, which would also allow a 'T& value()'
> signature. (Maybe the 'const' version could still be as above, depending
> whether or not it's felt useful to avoid the return value being a copy.)
I disagree. Existing "value" methods in Qt are all const and do not abort.
http://qt-project.org/doc/qt-5/qmap.html#value
http://qt-project.org/doc/qt-5/qhash.html#value
http://qt-project.org/doc/qt-5/qvector.html#value
http://qt-project.org/doc/qt-5/qlist.html#value
(missing for QLinkedList, what a surprise)
(missing for QContiguousCache)
QString is special: operator[] is allowed to reference out-of-bounds and
at(int) returns 0.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
More information about the Development
mailing list