The C++ Explicit Keyword and Qt

preview_player
Показать описание
C++ has a keyword called explicit, but why are there no implicit keywords?

Further how can it possibly be needed in the context of QObjects? And is there any tooling available that can help me ensure that it is everywhere, or that I at least have thought carefully about it, in those places where it should be?

Get these answers and many more in this episode where I talk to Peppe.

0:00 Inbtroduction
1:46 Implicit conversion in a string class
5:00 implicit conversion going wrong
8:00 the explicit keywords to the rescue
9:29 Named constructors
11:00 Why is explicit a problem in the contest of QObject sub-classes?
19:44 Should I always use explicit?
23:33 Q_IMPLICIT
24:33 explicit(false)
25:48 Implementation of Q_IMPLICIT
26:39 clang-tidy support
31:16 When should I not use explicit - QPoint
34:13 Rule of thumb when implicit is OK

Links:

All Qt Widgets and more videos:

About the host:
The video is presented by Jesper Pedersen, who started with Qt back when Qt was widgets only and the version was 1.44, which gives him 21 years of experience with Qt. Since 2000 he has taught almost 100 Qt classes, ranging from Qt Widgets to QML. Heck, Jesper even taught a few classes on QTopia Core — Trolltech's initial attempt at creating a phone stack. Today Jesper holds a fancy business title, but that doesn't prevent him from coding both in his job and in his spare time.

About KDAB:

KDAB experts regularly take time out to deliver KDAB’s world class training, in-house or at open enrollment courses around the world. We are the market leaders for training in Qt, OpenGL and C++.

Рекомендации по теме
Комментарии
Автор

13:45 and 19:00 are excellent examples to the point! They are real eye-openers for the need of explicit keyword. Amazing video, thank you both!

vladimirkraus
Автор

Clazy kept complaining about not declaring constructors as explicit and I've been adding them without knowing why, figures it's a good thing after all Hhhhh

Thank you for the explanation, although I gotta re-watch this later to understand it way better.

YoussefBeddad
Автор

Nice video, but I do see issues with that QPoint & drawline example. I don’t find it obvious it’s a pair of points, and not say a point and a 2D vector which could be expressed in the same way. The same if this were nor drawLine but drawRect: is that second argument a point, or a size? I can’t tell that from reading the code at the place of usage, and as Jesper said earlier in the video: that’s very important. Making the constructor of QPoint (and QSize, QVector2d, …) explicit forces us to write what we mean to do, and suddenly we understand the difference between drawLine(QPoint{1, 2}, QPoint{2, 2}) versus drawLine(QPoint{1, 2), QVector2d{2, 2})

AndreSomers
Автор

Thumbnail should have had a "Parental Advisory Explicit Content" label on it. You missed a trick there.

KX
welcome to shbcf.ru