Add chapter about auto

Fri, 08 Sep 2017 11:25:02 +0200

author
David Demelier <markand@malikania.fr>
date
Fri, 08 Sep 2017 11:25:02 +0200
changeset 13
4d6bf8b3446d
parent 12
ba9d0618d0ff
child 14
55bb3689093f

Add chapter about auto

STYLE_CPP.md file | annotate | diff | comparison | revisions
--- a/STYLE_CPP.md	Fri Sep 08 11:21:08 2017 +0200
+++ b/STYLE_CPP.md	Fri Sep 08 11:25:02 2017 +0200
@@ -41,7 +41,7 @@
 
 And a lambda has its braces on the same lines too:
 
-    sort([&] (object&) {
+    sort([&] (auto&) {
         return true;
     });
 
@@ -337,3 +337,23 @@
 
   - Do never put parentheses between the returned value,
   - Do not put a else branch after a return.
+
+### Auto
+
+We encorage usage of `auto`, it reduces code maintainance as you don't need to
+change your code when your rename types.
+
+````cpp
+auto it = std::find_if(v.begin(), v.end(), [&] (const auto& obj) {
+    return obj.key() == "foo";
+});
+
+for (const auto& pair : a_map)
+    std::cout << pair.first << " = " << pair.second << std::endl;
+````
+
+But do not use `auto` to write code like in python, this is not acceptable:
+
+````cpp
+    auto o = my_object("foo");
+````

mercurial