Krita/C++11: Difference between revisions

From KDE Community Wiki
(List some features which will probably not be used)
(Write about rvalue references)
Line 1: Line 1:
= General links to about using C++11 =
= General links to about using C++11 =
There have been a few links discussing mixing C++11 with Qt, and starting with Qt 5.6 C++11 support will be default. '''Note:''' as would be expected from those interested in hot new technology, the writers of most of these links demonstrate Silicon Valley levels of extreme optimism.  Take this with a grain of salt: there is no such thing as a free lunch.
There have been a few links discussing mixing C++11 with Qt, and starting with Qt 5.6 C++11 support will be default. ''Note:'' as would be expected from those interested in hot new technology, the writers of most of these links demonstrate Silicon Valley levels of extreme optimism.  Take this with a grain of salt: there is no such thing as a free lunch.


* [http://www.ics.com/blog/qt-and-c11  ICS.com]
* [http://www.ics.com/blog/qt-and-c11  ICS.com]
Line 6: Line 6:
* [http://woboq.com/blog/cpp11-in-qt5.html woboq.com: c++11 in Qt5]
* [http://woboq.com/blog/cpp11-in-qt5.html woboq.com: c++11 in Qt5]
* [http://woboq.com/blog/cpp14-in-qt.html woboq.com: c++14 in Qt5]
* [http://woboq.com/blog/cpp14-in-qt.html woboq.com: c++14 in Qt5]
* [http://artandlogic.com/2013/09/qt-5-and-c11-lambdas-are-your-friend/ "C++11 lambdas are your friend"]
* [http://www.dvratil.cz/2015/06/qt-containers-and-c11-range-based-loops/ Warning: c++11 range-based for may be slower]
* [https://archive.fosdem.org/2013/schedule/event/introcplusplus11/attachments/slides/203/export/events/attachments/introcplusplus11/slides/203/fosdem2013_cpp11.pdf FOSDEM 2013 presentation slides]
* [https://archive.fosdem.org/2013/schedule/event/introcplusplus11/attachments/slides/203/export/events/attachments/introcplusplus11/slides/203/fosdem2013_cpp11.pdf FOSDEM 2013 presentation slides]
* [https://www.kdab.com/wp-content/uploads/stories/slides/DD12/mutz-dd-speed-up-your-qt-5-programs-using-c++11.pdf KDAB: speed up your Qt 5 programs using C++11]
* [http://www.stroustrup.com/C++11FAQ.html Bjarne Stroustrup's C++11 FAQ] - the grand daddy
* [http://www.stroustrup.com/C++11FAQ.html Bjarne Stroustrup's C++11 FAQ] - the grand daddy


Line 16: Line 13:


=== Type Inference (auto) ===
=== Type Inference (auto) ===
'''Motivation:'''
''Motivation:''


'''Drawbacks:'''
''Drawbacks:''


'''Recommendation:'''
''Recommendation:''
=== Range-based for loop ===
=== Range-based for loop ===
'''Motivation:'''
''Motivation:''  


'''Drawbacks:'''
''Drawbacks:''  
[http://www.dvratil.cz/2015/06/qt-containers-and-c11-range-based-loops/ Warning: c++11 range-based for may be slower]


'''Recommendation:'''
''Recommendation:''
=== General Initializer Lists ===
=== General Initializer Lists ===
'''Motivation:'''
''Motivation:''
Member Initializers
Member Initializers
Mixed uniform initialization
Mixed uniform initialization


'''Drawbacks:'''
''Drawbacks:''


'''Recommendation:'''
''Recommendation:''
=== Lambdas ===
=== Lambdas ===
'''Motivation:'''
''Motivation:''
Lambda expressions for slots
Lambda expressions for slots
[http://artandlogic.com/2013/09/qt-5-and-c11-lambdas-are-your-friend/ "C++11 lambdas are your friend"]


'''Drawbacks:'''
''Drawbacks:''


'''Recommendation:'''
''Recommendation:''
=== constexpr ===
=== constexpr ===
'''Motivation:'''
''Motivation:'' Can speed things up inside hot loops. [https://www.kdab.com/wp-content/uploads/stories/slides/DD12/mutz-dd-speed-up-your-qt-5-programs-using-c++11.pdf KDAB: speed up your Qt 5 programs using C++11]


'''Drawbacks:'''
''Drawbacks:''


'''Recommendation:'''
''Recommendation:''
=== Override and final ===
=== Override and final ===
'''Motivation:'''
''Motivation:''


'''Drawbacks:'''
''Drawbacks:''


'''Recommendation:'''
''Recommendation:''
=== <algorithm> ===
=== <algorithm> ===
'''Motivation:'''
''Motivation:''


'''Drawbacks:'''
''Drawbacks:''


'''Recommendation:'''
''Recommendation:''


=== enum class ===  
=== enum class ===  
'''Motivation:'''
''Motivation:''


'''Drawbacks:'''
''Drawbacks:''


'''Recommendation:'''
''Recommendation:''


=== nullptr ===
=== nullptr ===
'''Motivation:'''
''Motivation:''


'''Drawbacks:'''
''Drawbacks:''


'''Recommendation:'''
''Recommendation:''
=== Deleted and default class members ===
=== Deleted and default class members ===
'''Motivation:'''
''Motivation:''


'''Drawbacks:'''
''Drawbacks:''


'''Recommendation:'''
''Recommendation:''
=== unique_ptr ===
=== unique_ptr ===
'''Motivation:'''
''Motivation:''


'''Drawbacks:''' unique_ptr is unable to be copied, and therefore cannot be used inside Qt classes.  For example QVector<std::unique_ptr> is invalid, because QVector requires its members can be copied. This makes converting to unique_ptr a bit slow, since QVector<T *>  will have to be converted to std_array<unique_ptr<T*>>, and the owner will no.   
''Drawbacks:'' unique_ptr is unable to be copied, and therefore cannot be used inside Qt classes.  For example QVector<std::unique_ptr> is invalid, because QVector requires its members can be copied. This makes converting to unique_ptr a bit slow, since QVector<T *>  will have to be converted to std_array<unique_ptr<T*>>. If the owner was being copied before, it will become uncopiableThis can be a good thing, but it is work.


'''Recommendation:'''
''Recommendation:''


== Performance-related (rvalues) ==  
== Performance-related (rvalues) ==  
http://thbecker.net/articles/rvalue_references/section_01.html
Using move constructors and rvalues are very subtle and advanced features, but widely celebrated as successes of C++11.  In general these should be used inside hot paths and when we sling around objects from place to place.  C++ programmers should already be aware that writing performant code inside hot paths will sometimes require slinging around ampersands.  These features will naturally stay confined to the corners of the codebase where they belong, and should be introduced when they are useful.
 
* [http://thbecker.net/articles/rvalue_references/section_01.html Tutorial for rvalue references]
* [https://www.kdab.com/wp-content/uploads/stories/slides/DD12/mutz-dd-speed-up-your-qt-5-programs-using-c++11.pdf KDAB: speed up your Qt 5 programs using C++11]
=== Move Constructors ===
=== Move Constructors ===
'''Motivation:'''
''Motivation:''


'''Drawbacks:'''
''Drawbacks:''


'''Recommendation:'''
''Recommendation:''
=== Reference Qualifiers ===
=== Reference Qualifiers ===
'''Motivation:'''
''Motivation:''


'''Drawbacks:'''
''Drawbacks:''


'''Recommendation:'''
''Recommendation:''


== C++11 features mostly for template programming ==  
== C++11 features mostly for template programming ==  

Revision as of 02:13, 15 October 2015

General links to about using C++11

There have been a few links discussing mixing C++11 with Qt, and starting with Qt 5.6 C++11 support will be default. Note: as would be expected from those interested in hot new technology, the writers of most of these links demonstrate Silicon Valley levels of extreme optimism. Take this with a grain of salt: there is no such thing as a free lunch.

Particular Features

Under "drawbacks," every item should list: "Programmers will face another feature they must learn about."

Type Inference (auto)

Motivation:

Drawbacks:

Recommendation:

Range-based for loop

Motivation:

Drawbacks: Warning: c++11 range-based for may be slower

Recommendation:

General Initializer Lists

Motivation: Member Initializers Mixed uniform initialization

Drawbacks:

Recommendation:

Lambdas

Motivation: Lambda expressions for slots "C++11 lambdas are your friend"

Drawbacks:

Recommendation:

constexpr

Motivation: Can speed things up inside hot loops. KDAB: speed up your Qt 5 programs using C++11

Drawbacks:

Recommendation:

Override and final

Motivation:

Drawbacks:

Recommendation:

<algorithm>

Motivation:

Drawbacks:

Recommendation:

enum class

Motivation:

Drawbacks:

Recommendation:

nullptr

Motivation:

Drawbacks:

Recommendation:

Deleted and default class members

Motivation:

Drawbacks:

Recommendation:

unique_ptr

Motivation:

Drawbacks: unique_ptr is unable to be copied, and therefore cannot be used inside Qt classes. For example QVector<std::unique_ptr> is invalid, because QVector requires its members can be copied. This makes converting to unique_ptr a bit slow, since QVector<T *> will have to be converted to std_array<unique_ptr<T*>>. If the owner was being copied before, it will become uncopiable. This can be a good thing, but it is work.

Recommendation:

Performance-related (rvalues)

Using move constructors and rvalues are very subtle and advanced features, but widely celebrated as successes of C++11. In general these should be used inside hot paths and when we sling around objects from place to place. C++ programmers should already be aware that writing performant code inside hot paths will sometimes require slinging around ampersands. These features will naturally stay confined to the corners of the codebase where they belong, and should be introduced when they are useful.

Move Constructors

Motivation:

Drawbacks:

Recommendation:

Reference Qualifiers

Motivation:

Drawbacks:

Recommendation:

C++11 features mostly for template programming

Krita makes very light use of templates. These features are useful, coming across them in the code base will add complexity for new learners, and have not been necessary so far.

  • decltype
  • static_assert
  • std::function and std::bind
  • variadic templates

Other C++11 features that probably cannot be used

  • Threads (Qt/KDE use an incompatible threading model)
  • shared_ptr and weak_ptr (Relies on C++ threading model; use KisSharedPointer)
  • New literal types (already have QString)
  • Extended Unions (already have QVariant)