Upgrade to Pro — share decks privately, control downloads, hide ads and more …

10 Reasons why modern C++ is not your grandfath...

10 Reasons why modern C++ is not your grandfather's C++

C++ has evolved a lot in the past decade, starting with the C++11 standard and up to C++17 in recent years.

C++'s reputation from the late 90s still sticks around and in this talk I want to try to dispel some of the myths surrounding the language.

This talk is aimed at Junior to Intermediate Programmers.

Ólafur Waage

January 03, 2019
Tweet

More Decks by Ólafur Waage

Other Decks in Programming

Transcript

  1. Ólafur Waage Graduated from University of Reykjavík in 2012 Worked

    at Tern doing ATC systems in C++ Currently working as a Senior Programmer at Ubisoft Massive in Malmö, Sweden
  2. The focus today is to go over many of the

    changes the languages has gotten in the past years.
  3. The focus today is to go over many of the

    changes the languages has gotten in the past years. This is not even slightly everything.
  4. The focus today is to go over many of the

    changes the languages has gotten in the past years. This is not even slightly everything. And the order is not important.
  5. The code shown today compiles, no pseudo code, but it

    might be missing some includes. But it’s all “Slideware”, and I use `int` as a type a lot, but what I’m talking about applies to many types, int is just convenient as an example.
  6. 10

  7. C++11 Introduced the ranged-based for loop Any expression that represents

    a suitable sequence (either an array or an object for which begin and end member functions or free functions are defined)
  8. 9

  9. auto is not magic auto is the exact type on

    the left hand side auto is known at compile time
  10. auto is not magic auto is the exact type on

    the left hand side auto is known at compile time BUT
  11. auto is not magic auto is the exact type on

    the left hand side auto is known at compile time BUT auto might be a type you didn’t expect auto might obscure readability in some cases
  12. 8

  13. Common responses to “Why not C++?” “I don’t want to

    worry about memory leaks” “Manual memory management is too complex”
  14. Common responses to “Why not C++?” “I don’t want to

    worry about memory leaks” “Manual memory management is too complex” “And have to deal with pointers? No thanks”
  15. This idea of wrapping a scary operation into a class

    is well supported in C++, it’s even encouraged.
  16. This idea of wrapping a scary operation into a class

    is well supported in C++, it’s even encouraged. The technique is called RAII (Resource acquisition is initialization)
  17. This idea of wrapping a scary operation into a class

    is well supported in C++, it’s even encouraged. The technique is called RAII (Resource acquisition is initialization) (best name ever…)
  18. When you open a file, you need to close it.

    There we have an idea of a resource that we acquire, where we have to return it for the system to function properly.
  19. When you open a file, you need to close it.

    There we have an idea of a resource that we acquire, where we have to return it for the system to function properly. Regardless of what happened when we asked for the resource.
  20. This is too much and I don’t care. Just open

    a file, let me read from it, give me checks if it went ok and release it if/when we have to.
  21. “Ok, great... But I don’t want to write all this.”

    This is what std::vector and any similar containers do. (list, map, set)
  22. “Ok, great... But I don’t want to write all this.”

    This is what std::vector and any similar containers do. (list, map, set) “But I don’t need a container, I just need memory”
  23. std::unique_ptr<T> Is a wrapper around a T* Only a single

    owner can exist, ownership needs to be transferred. Any ill behaving code does not compile.
  24. std::shared_ptr<T> Is a wrapper around T* and an int counter

    Many can share this pointer, when copied, the counter is incremented, if the structure goes out of scope, the counter is decremented, if the counter is zero, the T is deleted.
  25. With types like this (and other new STL types and

    containers), you want to be able to ask questions about them, to be safer about what they are and what they do.
  26. 7

  27. When certain criteria are met, an implementation is allowed to

    omit the copy construction of a class object.
  28. When certain criteria are met, an implementation is allowed to

    omit the copy construction of a class object. Even if the copy constructor and/or destructor for the object have side effects.
  29. In such cases, the implementation treats the source and target

    of the omitted copy operation as simply two different ways of referring to the same object
  30. This is called Copy elision RVO - When dealing with

    temporaries NRVO - When dealing with named variables
  31. 6

  32. Rvalues “An rvalue is a temporary value that does not

    persist beyond the expression that uses it.”
  33. What is this whole move semantics thing then? “I’m glad

    you asked!” The keyword here is Semantics. Even though there is new syntax going on, it’s not always needed. The semantics still apply.
  34. The main idea is about turning lvalues into rvalues. This

    signals to the compiler, this used to be an lvalue, but I don’t need it anymore. Do with it what you will. And it’s that signaling that makes it important.
  35. 5

  36. Those structures are called closures. A lambda is defined as:

    “The lambda expression is a expression of unique unnamed non-union non-aggregate class type, known as closure type”
  37. You might need in some cases to capture variables into

    the scope of the lambda. You have to specify the capture directly.
  38. 4

  39. std::function A general-purpose polymorphic function wrapper. Instances of std::function can

    store, copy, and invoke any Callable target -- functions, lambda expressions, or other function objects.
  40. 3

  41. The main point is not about optimizations that can happen,

    it’s about communicating to the compiler your intent. “If you see a possibility to optimize this into a constant expression, do it”
  42. 2

  43. Asia / Pacific C++ and System Software Summit - China

    CppSiberia - Russia PacifiC++ - Australia/New Zealand North America C++Now - US CppCon - US Qt World Summit - US/Europe US LLVM Developers Meeting - US
  44. Europe ACCU - UK Audio Developers Conference - UK code::dive

    - Poland Core C++ - Israel CoreHard - Belarus CppRussia - Russia emBO++ - Germany EuroLLVM - Europe Italian CppCon - Italy Meeting C++ - Germany NDC TechTown Qt World Summit - US/Europe usingstdcpp - Spain
  45. 1

  46. The landscape of C++ tools has greatly shifted in the

    past years. IDE’s getting very advanced features.
  47. The landscape of C++ tools has greatly shifted in the

    past years. IDE’s getting very advanced features. Introduction of clang and all it’s tools.
  48. The landscape of C++ tools has greatly shifted in the

    past years. IDE’s getting very advanced features. Introduction of clang and all it’s tools. And many new online utilities have appeared.
  49. 0

  50. Simpler for loops allow us to iterate through collections with

    less typing. auto shortens a lot of types we would have otherwise had to specify.
  51. Simpler for loops allow us to iterate through collections with

    less typing. auto shortens a lot of types we would have otherwise had to specify. Memory leaks are now harder to achieve with the new smart pointers.
  52. We can worry less about objects being copied all over

    the place. We can move values between owners in a more performant way than before.
  53. We can worry less about objects being copied all over

    the place. We can move values between owners in a more performant way than before. We can create simple lambdas and store them in function objects to be executed later
  54. We can create and specify constant expressions New and improved

    tools help us reason about our codebase and help us catch bugs early on
  55. We can create and specify constant expressions New and improved

    tools help us reason about our codebase and help us catch bugs early on The C++ community is very active and nice, which is pretty good in my opinion.