0

The similar question can be found here.

I want to pass a null QString to void foo(const QString &). The standard answer from many resources, examples and Qt docs is:

foo(QString());

However, can I consider the following initialization as a better replacement in C++11 and later?

foo({});

It's basically a shorter notation, however probably there are some pitfalls or caveats?

John Doe
  • 555
  • 6
  • 17
  • Remember that QString distinguishes between an empty string and a *null* string. See the documentation for `isEmpty` Vs `isNull`. – Jesper Juhl Jan 24 '20 at 19:07
  • @JesperJuhl Yeah, I actually need a *null* string, not an empty (`""`). – John Doe Jan 24 '20 at 19:08
  • 1
    C++11 has nothing to do with it though. – Jesper Juhl Jan 24 '20 at 19:14
  • 1
    Since `foo` has no other overloads and it's parameter's type is strictly defined the only thing I can see that might go wrong is if `QString` gets an initializer list constructor in later versions of Qt that won't initialize it to null. This seems very unlikely to me. Edit : This assumes `foo` won't change. If `foo` is liable of changing in the future or getting additional overloads it may also cause `{}` to behave differently or fail to compile. – François Andrieux Jan 24 '20 at 19:18
  • @JesperJuhl For some reason I thought that curly braces initialization was introduced in C++11. Edited my question. – John Doe Jan 24 '20 at 19:26
  • 1
    @JohnDoe It was. – François Andrieux Jan 24 '20 at 19:27
  • @FrançoisAndrieux Thanks a lot! But why I don't often see this type of initialization in docs, sources, etc.? Is it because of the potential ambiguity when there are multiple overloads (which you mentioned previously)? Also, could you please add your comment as an answer so I could upvote and accept it? – John Doe Jan 24 '20 at 19:30
  • @FrançoisAndrieux "It was". Then I can't see a point in Jesper Juhl's remark regarding "C++ has nothing to do with it though". – John Doe Jan 24 '20 at 19:33
  • @JohnDoe I'm not 100% confident my answer is correct (notice the *"...the only thing I can see..."*) Its possible I missed a scenario. So I would rather not post it as an answer. Another factor for why this kind of initialization is not seen may be that it is more recent and less well understood. Not only does it mean fewer people *can* use the technique, it also means that there is a question of readability. If there is doubt as to whether or not your colleague will understand it, then you have to consider the impact that will have as well. – François Andrieux Jan 24 '20 at 19:35
  • @JohnDoe I think it was okay to have the c++11 tag. The question does ask about a feature introduced by that version. And I bet Jesper was not confident that it should be retracted either (otherwise they could have removed it themselves). But truthfully, since the answer isn't *constrained* to c++11 (imagine a scenario where the answer changes with newer versions of c++) then it isn't strictly needed either. – François Andrieux Jan 24 '20 at 19:37
  • My point was simply that you don't need C++11 to use null QString's. – Jesper Juhl Jan 24 '20 at 19:59
  • @JesperJuhl Oh, I see. However, the question is focused on the possible drawbacks of `{}` (introduced in C++11) initialization versus `QString()`. – John Doe Jan 25 '20 at 04:36

0 Answers0