Importance of understanding C++ features of 11, 17, and beyond
-
Compared to the classic form of the C++, how important are the new features included in C++11 and successors, in terms of understanding how to write an application in Qt that would tend to approach general standards of code quality and best practices?
-
@brainchild said in Importance of understanding C++ features of 11, 17, and beyond:
Compared to the classic form of the C++, how important are the new features included in C++11 and successors
If you're stuck with pre- C++11 nowadys then you will have a hard time to understand any new c++ code - no matter if it's in the Qt library or somewhere else. Don't understand what you want to know here...
-
@brainchild said in Importance of understanding C++ features of 11, 17, and beyond:
Compared to the classic form of the C++, how important are the new features included in C++11 and successors
If you're stuck with pre- C++11 nowadys then you will have a hard time to understand any new c++ code - no matter if it's in the Qt library or somewhere else. Don't understand what you want to know here...
How prominently do design patterns in Qt overlap features added in the more recent version of C++? I have understood many to be related to more flexible use of templates, and more efficient passing of references. Perhaps such constructs are less important in a typical Qt application than in certain others. Such background was the motivation for my question.
-
How prominently do design patterns in Qt overlap features added in the more recent version of C++? I have understood many to be related to more flexible use of templates, and more efficient passing of references. Perhaps such constructs are less important in a typical Qt application than in certain others. Such background was the motivation for my question.
@brainchild said in Importance of understanding C++ features of 11, 17, and beyond:
Perhaps such constructs are less important in a typical Qt application than in certain others
Still don't understand what you mean - templates are used in C++ code (again - look in the sources) and also const references are no new c++11 feature and are used since Qt1 times to avoid copying data.
-
Qt6 requires C++17 features to build, so understanding these is essential if you are going to hack Qt itself.
For programs using Qtnullptr
, initializer lists, and lambdas in connect() calls from C++11 are quite useful.I do not think that Qt presently requires any of these new language features of C++17 in user code. Happy to be corrected though.
What passes for "code quality" and "best practice" is, to some degree, in the eye of the beholder.
-
Qt6 requires C++17 features to build, so understanding these is essential if you are going to hack Qt itself.
For programs using Qtnullptr
, initializer lists, and lambdas in connect() calls from C++11 are quite useful.I do not think that Qt presently requires any of these new language features of C++17 in user code. Happy to be corrected though.
What passes for "code quality" and "best practice" is, to some degree, in the eye of the beholder.
First, to clarify, I am not referring to the tool chain or code compatibility.
Some of the recent enhancements have been oriented around very abstract and perhaps obtuse problems of efficiency remaining from the early language design. An example of such an enhancement is perfect forwarding. It is necessary to be up to speed with these nuances to avoid writing code in a Qt application that peers may consider below standards?
-
First, to clarify, I am not referring to the tool chain or code compatibility.
Some of the recent enhancements have been oriented around very abstract and perhaps obtuse problems of efficiency remaining from the early language design. An example of such an enhancement is perfect forwarding. It is necessary to be up to speed with these nuances to avoid writing code in a Qt application that peers may consider below standards?
@brainchild said in Importance of understanding C++ features of 11, 17, and beyond:
. It is necessary to be up to speed with these nuances to avoid writing code in a Qt application that peers may consider below standards?
It depends on your peers.
I would expect that you understand such stuff when you want to be a c++ programmer. But this has nothing to do with Qt.
If you don't want to use a language feature then don't use it. -
@brainchild said in Importance of understanding C++ features of 11, 17, and beyond:
. It is necessary to be up to speed with these nuances to avoid writing code in a Qt application that peers may consider below standards?
It depends on your peers.
I would expect that you understand such stuff when you want to be a c++ programmer. But this has nothing to do with Qt.
If you don't want to use a language feature then don't use it.I understand. The kind of answers I was seeking would be ones based on practical experience from writing in Qt.
-
I understand. The kind of answers I was seeking would be ones based on practical experience from writing in Qt.
@brainchild said in Importance of understanding C++ features of 11, 17, and beyond:
The kind of answers I was seeking would be ones based on practical experience from writing in Qt.
?
If you want to use a c++ library you should know the language features used by this library. Not that you should be able to write such stuff by your own to perfection.
-
I understand. The kind of answers I was seeking would be ones based on practical experience from writing in Qt.
@brainchild Qt 6 requires only C++17 and is well behind C++ development. For example, on Ubuntu 22.04, you can mix C++20 or 23 features into your Qt app. It is up to you. I will definitely do it.
-
When confronted with code I don't understand in C++. I go to cppreference.com. I usually get a basic understanding of what it is supposed to be doing. Say to myself, "that is neat". Then, if it is useful, I might use that in my new code if it solves a problem. Most of the time it is a detail that I might need in specific circumstances. So the next time I might need it I usually have to look it up again to get the syntax right. But at least I know the concept of why it exists. A good example of this is forcing a class to no longer do a virtual lookup for a virtual function. I "might" need this in an embedded context. Most of the time I don't and any speedup is not worth using that feature.
As for basic knowledge to use with Qt. I would say at least a good familiarity with C++ 11. Being able to use and understand what templates are doing is important. Understanding the differences between Qt and std objects is important as well. Not knowing when a Qt object might detach could have performance impacts. But a lot of this you pickup as you learn both C++ and Qt.
-
@brainchild your question really has multiple answers: one for C++11, one for 17 and one for beyond. As a relatively late adopter of most programming advancements, I can vouch for the value of learning C++11. In the several Qt applications I've developed and delivered, I've relied to varying degrees on 11. I think it's safe to say that it's quite "mainstream," both as a tool for Qt programming as well as the overall coding community.
I can't speak authoritatively to C++17 or beyond. As a rule of thumb, though, when the Qt developers adopt a new technology, they're not doing so because it's cool -- they're doing so because in their opinions it makes for better code. I'm in no position to disagree with them.
-
@brainchild Qt 6 requires only C++17 and is well behind C++ development. For example, on Ubuntu 22.04, you can mix C++20 or 23 features into your Qt app. It is up to you. I will definitely do it.
@JoeCFD said in Importance of understanding C++ features of 11, 17, and beyond:
@brainchild Qt 6 requires only C++17 and is well behind C++ development.
The first part is correct, the second is plain wrong.
The minimal C++ standard is raised rarely so as to allow recent versions of Qt to run on older systems and as the name suggests, it's only a minimal. You can use whichever higher version of the standard your compiler supports.
-
Compared to the classic form of the C++, how important are the new features included in C++11 and successors, in terms of understanding how to write an application in Qt that would tend to approach general standards of code quality and best practices?
@brainchild If you are writing C++ software in 2022, it is probably helpful to understand C++ as it exists in 2022. I'm not sure how any other answer could really arise. There's no specific reason to write code as if it was 20 years ago unless you are doing a retrocomputing project as a hobby.
This answer applies broadly to all languages/frameworks/API's/libraries. You wouldn't write Python 1.X today, nor JavaScript for Netscape Navigator 3.
-
Thanks for the answers so far. Many have been helpful.
I realize it has been unhelpful that I have not provided more texture concerning the reason for asking the question.
One approach for building software is to write whatever works first, then fix it later. Another is to understand common pitfalls first, and then at least try to avoid ones that may be anticipated. Many experienced developers prefer the first, but many of them also have enough background in any particular language, platform, or toolchain, that they need not fear the most basic mistakes.
In C++, I have often found many approaches occur for the same problem, but most of the ones that work are still rejected by experts. With the recent enhancements, I think the situation is exacerbated, with so many ways, for example, to copy an object too many times, even though there is no detectable problem, because a copy looks the same as the original at runtime, and the copy operation is implicit in the language semantics.
It is always undesirable to write code that peers reject due to its being dated or incorrect.
-
In todays' world you probably don't get around lambda functions, move and rvalue reference, and the for-each loop. E.g., lambda functions can be used as slots in connect statements. This is back from C++11 which is already 11 years old. You should also know about return value optimization and copy elision because this heavily influences modern code style (returning a large value object was kind of forbidden in classic C++). Maybe this is all you need to know when interfacing with Qt's API. However, most source code has a lot more code that is not Qt-specific. For all your logic and so on you should try to keep up with modern C++. For example, you should use concepts whenever you can, if you use the appropriate standard already. We are still at C++17 for now, but there are a few features of newer standards that we really would like to use. std::map::contains() and std::string::contains() would be wonderful...
-
In todays' world you probably don't get around lambda functions, move and rvalue reference, and the for-each loop. E.g., lambda functions can be used as slots in connect statements. This is back from C++11 which is already 11 years old. You should also know about return value optimization and copy elision because this heavily influences modern code style (returning a large value object was kind of forbidden in classic C++). Maybe this is all you need to know when interfacing with Qt's API. However, most source code has a lot more code that is not Qt-specific. For all your logic and so on you should try to keep up with modern C++. For example, you should use concepts whenever you can, if you use the appropriate standard already. We are still at C++17 for now, but there are a few features of newer standards that we really would like to use. std::map::contains() and std::string::contains() would be wonderful...
Thanks. Now I have some very clear details on what to make a priority.