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

Marco Bubke Marco.Bubke at qt.io
Wed Aug 24 14:07:24 CEST 2016


I really like the idea. Do we could add std::string_view too? [?]

________________________________
From: Qt-creator <qt-creator-bounces+marco.bubke=qt.io at qt-project.org> on behalf of Eike Ziller <Eike.Ziller at qt.io>
Sent: Wednesday, August 24, 2016 1:57:45 PM
To: qt-creator at qt-project.org
Subject: [Qt-creator] Proposal: Adding std::(experimental::)optional

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




_______________________________________________
Qt-creator mailing list
Qt-creator at qt-project.org
http://lists.qt-project.org/mailman/listinfo/qt-creator
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/qt-creator/attachments/20160824/42d0fe3c/attachment.html>


More information about the Qt-creator mailing list