[Qt-creator] Proposal: Adding std::(experimental::)optional

Eike Ziller Eike.Ziller at qt.io
Wed Aug 24 13:57:45 CEST 2016


Proposal: Adding std::(experimental::)optional

tl;dr: Proposal is to pull in the reference implementation for std::optional into Qt Creator, and start experimenting with its usage.
https://codereview.qt-project.org/165120

optional provides a way for variables to optionally contain a value or not.
Some types in C++ already have an implicit “non-value”, e.g. nullptr is often used to express that there is no value for a pointer type. Some Qt classes have ::isValid or ::isNull. optional makes explicit in the API that “no value” is a valid condition and should be checked.

- "optional<IEditor*> EditorManager::currentEditor() const” makes it explicit that not having a current editor is a reasonable expectation, and that you have to check this condition before doing anything with the potential IEditor instance.
- On the other hand, IEditor instances without an IDocument are not allowed, so if you are checking the return value of “IDocument *IEditor::document() const”, you should do that only as part of a QTC_ASSERT/QTC_GUARD style sanity check.

optional also enables optional values for non-pointer types as well, often removing the need of special “isValid” functions and special value return types.

- “optional<int> parseInt(const QString &s)” instead of “int parseInt(const QString &s, bool *ok)”

- “optional<quint16> Device::port() const” instead of a whole Utils::Port class with a isValid() method internally misusing the value -1, including lots of comparison operators.

- "optional<int> BinEditorWidget::dataIndexOf(const QByteArray &pattern, qint64 from, bool caseSensitive) const” instead of "int BinEditorWidget::dataIndexOf(const QByteArray &pattern, qint64 from, bool caseSensitive) const” with special return value -1.

std::optional is part of C++17, and there is an implementation in Boost, and a reference implementation for the standard on https://github.com/akrzemi1/Optional . I’d like to pull the reference implementation into Qt Creator, and start experimenting with it in Qt Creator. (Probably not starting with such prominent places as EditorManager::currentEditor() though ;) )

https://codereview.qt-project.org/165120

-- 
Eike Ziller
Principal Software Engineer

The Qt Company GmbH
Rudower Chaussee 13
D-12489 Berlin
eike.ziller at qt.io
http://qt.io
Geschäftsführer: Mika Pälsi, Juha Varelius, Tuula Haataja
Sitz der Gesellschaft: Berlin, Registergericht: Amtsgericht Charlottenburg, HRB 144331 B






More information about the Qt-creator mailing list