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

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

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. 10 Reasons why modern C++ is not your grandfather's C++

  2. Ólafur Waage

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

    at Tern doing ATC systems in C++
  4. Ó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
  5. None
  6. None
  7. The focus today is to go over many of the

    changes the languages has gotten in the past years.
  8. 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.
  9. 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.
  10. The code shown today compiles, no pseudo code, but it

    might be missing some includes.
  11. 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.
  12. 10

  13. for loops

  14. None
  15. None
  16. None
  17. None
  18. None
  19. None
  20. None
  21. None
  22. 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)
  23. None
  24. None
  25. None
  26. None
  27. None
  28. None
  29. 9

  30. auto

  31. None
  32. None
  33. auto is not magic auto is the exact type on

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

    the left hand side auto is known at compile time BUT
  35. 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
  36. None
  37. None
  38. None
  39. None
  40. None
  41. None
  42. None
  43. None
  44. None
  45. None
  46. None
  47. None
  48. 8

  49. memory

  50. Common responses to “Why not C++?”

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

    worry about memory leaks”
  52. Common responses to “Why not C++?” “I don’t want to

    worry about memory leaks” “Manual memory management is too complex”
  53. 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”
  54. None
  55. None
  56. This idea of wrapping a scary operation into a class

    is well supported in C++, it’s even encouraged.
  57. 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)
  58. 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…)
  59. When you open a file, you need to close it.

  60. 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.
  61. 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.
  62. This is too much and I don’t care.

  63. 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.
  64. None
  65. None
  66. None
  67. None
  68. “Ok, great... But I don’t want to write all this.”

    This is what std::vector and any similar containers do. (list, map, set)
  69. “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”
  70. None
  71. None
  72. std::unique_ptr<T>

  73. std::unique_ptr<T> Is a wrapper around a T*

  74. 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.
  75. None
  76. None
  77. None
  78. None
  79. None
  80. std::shared_ptr<T>

  81. std::shared_ptr<T> Is a wrapper around T* and an int counter

  82. 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.
  83. None
  84. None
  85. None
  86. None
  87. None
  88. 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.
  89. With type traits you are able to

  90. With type traits you are able to

  91. 7

  92. copying

  93. None
  94. None
  95. None
  96. When certain criteria are met, an implementation is allowed to

    omit the copy construction of a class object.
  97. 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.
  98. 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
  99. None
  100. None
  101. None
  102. None
  103. None
  104. This is called Copy elision RVO - When dealing with

    temporaries NRVO - When dealing with named variables
  105. 6

  106. moving

  107. Lvalues “An lvalue is an expression whose address can be

    taken”
  108. Rvalues “An rvalue is a temporary value that does not

    persist beyond the expression that uses it.”
  109. None
  110. None
  111. What is this whole move semantics thing then?

  112. What is this whole move semantics thing then? “I’m glad

    you asked!”
  113. 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.
  114. 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.
  115. std::move();

  116. std::move(); Move doesn’t move…

  117. std::move(); Move doesn’t move…

  118. std::move(); Move doesn’t move…

  119. Enough! What does this mean though?

  120. None
  121. None
  122. None
  123. None
  124. None
  125. None
  126. None
  127. None
  128. None
  129. None
  130. None
  131. None
  132. None
  133. 5

  134. lambdas

  135. None
  136. None
  137. None
  138. None
  139. None
  140. None
  141. None
  142. None
  143. None
  144. Those structures are called closures.

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

  146. 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”
  147. Lambda syntax: [captures](params){ body }

  148. None
  149. None
  150. None
  151. None
  152. You might need in some cases to capture variables into

    the scope of the lambda.
  153. You might need in some cases to capture variables into

    the scope of the lambda. You have to specify the capture directly.
  154. None
  155. None
  156. None
  157. None
  158. None
  159. None
  160. 4

  161. functions

  162. “Wait a minute. Functions are already in the language. What

    are you on about?”
  163. std::function

  164. std::function A general-purpose polymorphic function wrapper.

  165. 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.
  166. None
  167. None
  168. None
  169. None
  170. None
  171. None
  172. None
  173. None
  174. None
  175. None
  176. None
  177. None
  178. None
  179. 3

  180. constexpr

  181. constexpr Specifies that the value of a variable or function

    can appear in constant expressions.
  182. None
  183. None
  184. None
  185. None
  186. None
  187. None
  188. 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”
  189. None
  190. 2

  191. community

  192. The value of a great and friendly community can not

    be measured.
  193. None
  194. 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
  195. 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
  196. None
  197. None
  198. None
  199. https://github.com/shafik/cpp_youtube_channels

  200. None
  201. 1

  202. tools

  203. “Where would I be without my hammer? Well, with a

    finger, that’s for sure”
  204. The landscape of C++ tools has greatly shifted in the

    past years.
  205. The landscape of C++ tools has greatly shifted in the

    past years. IDE’s getting very advanced features.
  206. 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.
  207. 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.
  208. Compiler Explorer - https://godbolt.org/

  209. Compiler Explorer - https://godbolt.org/

  210. Compiler Explorer - https://godbolt.org/

  211. Compiler Explorer - https://godbolt.org/

  212. clang-tidy

  213. Quick Bench - http://quick-bench.com

  214. Conan - https://conan.io/

  215. 0

  216. summary

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

    less typing.
  218. 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.
  219. 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.
  220. We can worry less about objects being copied all over

    the place.
  221. 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.
  222. 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
  223. We can create and specify constant expressions

  224. We can create and specify constant expressions New and improved

    tools help us reason about our codebase and help us catch bugs early on
  225. 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.
  226. Thank you! You can follow me @olafurw On Twitter