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

Eike Ziller Eike.Ziller at qt.io
Fri Aug 26 10:36:54 CEST 2016


> On Aug 26, 2016, at 10:03 AM, Eike Ziller <Eike.Ziller at qt.io> wrote:
> 
> 
>> On Aug 25, 2016, at 8:25 PM, Giuseppe D'Angelo <dangelog at gmail.com> wrote:
>> 
>> On Wed, Aug 24, 2016 at 1:57 PM, Eike Ziller <Eike.Ziller at qt.io> wrote:
>>> - "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.
>> 
>> Playing devil's advocate here: what's the point of using an optional
>> pointer? Pointers already have an optional value -- the null pointer
>> constant. That seems totally redundant. Ideally one would use
>> optional<IEditor&>, which doesn't compile for other reasons, but
>> would have the semantics you describe.

Actually one could say that optional<T&> _is_ T* ;)
So basically to make a difference in the API we’d need to change all pointers that are not optional to references.

> Pointers do have an optional value, but actually often nullptr is not a valid value for a pointer semantically. So IMO it would add API clarity.
> 
> In an imaginary world where optional would be used consistently, I’d know that if I want to implement an IEditor, that
> IDocument *MyEditor::document() const { return 0; }
> is not correct, simply because it is not optional<IDocument*> ;)
> 
> And I would know that
> if (IDocument *doc = someEditor->document()) ....
> is senseless.
> 
> Also we do not check every pointer that we get from a method for nullptr. And actually I do not see the sense in doing it (e.g. for IEditor::document() we definitely don’t).
> So even in the existing, non-ideal world I’d find out that
> project()->activeTarget()->activeBuildConfiguration()->buildDirectory()
> is just wrong, because both activeTarget and activeBuildConfiguration are optional, and the compiler doesn’t let me write it that way.
> 
> And, at the time that activeBuildConfiguration was made optional (it wasn’t all the time), all the code doing
> target->activeBuildConfiguration()->buildDirectory()
> which was perfectly right so far, would actually lead to a compile error because it is wrong now.
> 
> So I do see value of using optional even for pointers.
> 
> -- 
> 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

-- 
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