File tree 2 files changed +47
-0
lines changed
2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ Use of `auto `
3
+ =============
4
+
5
+ Use `auto ` only when the type is long, complex, or hard to write explicitly.
6
+
7
+ Examples where `auto ` is appropriate:
8
+
9
+ .. code-block :: cpp
10
+
11
+ auto it = container.begin(); // Iterator type is long and not helpful to spell out
12
+
13
+ // The return type is std::vector<std::vector<std::pair<int, float>>>
14
+ auto matrix = generate_adjacency_matrix();
15
+
16
+ // Lambdas have unreadable and compiler-generated types — use auto for them
17
+ auto add = [](int x, int y) { return x + y; };
18
+
19
+
20
+ Avoid `auto ` when the type is simple and clear:
21
+
22
+ .. code-block :: cpp
23
+
24
+ // Use type names when they are short and readable.
25
+ for (RRNodeId node_id : device_ctx.rr_graph.nodes()) {
26
+ t_rr_node_route_inf& node_inf = route_ctx.rr_node_route_inf[rr_id];
27
+ }
28
+
29
+ int count = 0;
30
+ std::string name = "example";
31
+ std::vector<int> numbers = {1, 2, 3};
32
+
33
+ Avoid:
34
+
35
+ .. code-block :: cpp
36
+
37
+ auto count = 0; // Simple and obvious type
38
+ auto name = std::string("x"); // Hides a short, clear type
39
+
40
+ for (auto node_id : device_ctx.rr_graph.nodes()) {
41
+ // node_id is RRNodeId. Write it out for clarity.
42
+ auto& node_inf = route_ctx.rr_node_route_inf[rr_id];
43
+ // node_inf is t_rr_node_route_inf. Use the explicit type since it's simple and meaningful.
44
+ }
45
+
46
+ Rationale: clear, explicit types help with readability and understanding. Avoid hiding simple types behind `auto `.
Original file line number Diff line number Diff line change @@ -11,5 +11,6 @@ Developer Guide
11
11
c_api_doc
12
12
code_documentation
13
13
tutorials/index
14
+ coding_style
14
15
../SUPPORT
15
16
../LICENSE
You can’t perform that action at this time.
0 commit comments