Initializer list syntax of subclass created with QtCreator class "wizard"
-
Hey guys,
I would say I have a solid C++ knowledge, but I'm not following every change or update to C++.
I've noticed that the QtCreator class wizard (QtC 13 with C17) went from:MyWidget::MyWidget(QWidget *parent) : QWidget(parent){}
as used in earlier versions
to:MyWidget::MyWidget(QWidget *parent) : QWidget{parent}{}
now.
So curly brackets (
{...}
) are used instead of parentheses ((...)
).Why that?
Dis-/Advantages?!
: QWidget(parent)
is still valid, but should one continue to use{parent}
from now on?Any explanation appreaciated :)
-
@Pl45m4 said in Initializer list syntax of subclass created with QtCreator class "wizard":
Why that?
The 'feature' (debatable how much of it is really a feature) is called uniform initialization. You may want to take a peek here [1].
: QWidget(parent) is still valid, but should one continue to use {parent} from now on?
Depends on your style and understanding. I don't, as I don't agree that I should mindlessly slap whatever the core guideline guidelines tells me to.
Dis-/Advantages?!
There are cases where that syntax has unexpected behaviour. If you have say:
class Foo { Foo(); Foo(std::initializer_list<Bar>); };
You might run in trouble doing:
Foo x{}; //< We call what exactly, is this the default constructor, or the one taking an initializer list?
But I will grant you, the example is contrived. Here's a case where you may hit this in Qt.
QJsonValue a{}; //< Do we mean to call QJsonValue(), or QJsonValue(QJsonObject(std::initializer_list<...>))
As it happens, due to well C++, we call the constructors that take an initializer list in both cases. All in all we shot ourselves in the foot. So, if you ask me - use
{}
when you mean to use an initializer list, all other cases - simply call the constructor. That is to say:std::vector x = { 1, 2, 3, 4 }; //< Just fine! std::vector<int> y(5) //< Creating a vector that has 5 elements!! int q = 4; //< Can't get more explicit than that. `int q(4);` and `int q{4};` are both valid, but is it clearer to read? I sure don't think so. Looks more like something you'd write to impress your colleagues.
Warning: Notice in the last code snippet that
std::vector<int> y(5)
is very, very different fromstd::vector<int> y{5}
[1]: https://isocpp.org/wiki/faq/cpp11-language#uniform-init
-
@Pl45m4 said in Initializer list syntax of subclass created with QtCreator class "wizard":
Why that?
The 'feature' (debatable how much of it is really a feature) is called uniform initialization. You may want to take a peek here [1].
: QWidget(parent) is still valid, but should one continue to use {parent} from now on?
Depends on your style and understanding. I don't, as I don't agree that I should mindlessly slap whatever the core guideline guidelines tells me to.
Dis-/Advantages?!
There are cases where that syntax has unexpected behaviour. If you have say:
class Foo { Foo(); Foo(std::initializer_list<Bar>); };
You might run in trouble doing:
Foo x{}; //< We call what exactly, is this the default constructor, or the one taking an initializer list?
But I will grant you, the example is contrived. Here's a case where you may hit this in Qt.
QJsonValue a{}; //< Do we mean to call QJsonValue(), or QJsonValue(QJsonObject(std::initializer_list<...>))
As it happens, due to well C++, we call the constructors that take an initializer list in both cases. All in all we shot ourselves in the foot. So, if you ask me - use
{}
when you mean to use an initializer list, all other cases - simply call the constructor. That is to say:std::vector x = { 1, 2, 3, 4 }; //< Just fine! std::vector<int> y(5) //< Creating a vector that has 5 elements!! int q = 4; //< Can't get more explicit than that. `int q(4);` and `int q{4};` are both valid, but is it clearer to read? I sure don't think so. Looks more like something you'd write to impress your colleagues.
Warning: Notice in the last code snippet that
std::vector<int> y(5)
is very, very different fromstd::vector<int> y{5}
[1]: https://isocpp.org/wiki/faq/cpp11-language#uniform-init
-