Skip to content

Commit ea33bcf

Browse files
authored
feat: add auto concept (#836)
1 parent d48c46c commit ea33bcf

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed

concepts/auto/.meta/config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"blurb": "The `auto` keyword in C++ allows automatic type deduction, simplfying code by letting the compiler infer variable types from their initializers.",
3+
"authors": [
4+
"vaeng"
5+
]
6+
}

concepts/auto/about.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# About
2+
3+
## The `auto` Keyword in C++
4+
5+
In C++, the `auto` keyword is a powerful feature introduced in C++11, used to declare variables with an inferred data type.
6+
The compiler deduces the type of the variable based on its initializer, which can make code more readable and easier to maintain.
7+
8+
## Example Usage
9+
10+
Consider the following example where `auto` is used to declare variables:
11+
12+
```cpp
13+
auto dragon_population{3}; // dragon_population is deduced as an integer
14+
auto westeros{7.7777}; // westeros is deduced as a double
15+
auto wedding_location{"The Twins"}; // wedding_location is deduced as a const char*, not std::string
16+
```
17+
18+
In each case, the type of the variable is inferred from the value it is initialized with.
19+
20+
## Type Inference
21+
22+
The `auto` keyword helps by writing more concise and readable code by reducing the verbosity of explicit types.
23+
24+
```cpp
25+
const std::vector<std::string> pigeon_pie{"flour", "butter", "pigeon", "salt"};
26+
auto purple_wedding_pie{pigeon_pie};
27+
purple_wedding_pie.emplace_back("the strangler");
28+
```
29+
30+
In this loop, `auto` deduces the type of `purple_wedding_pie` as `std::vector<std::string>`, avoiding the need to explicitly specify the type again.
31+
32+
## Compatibility
33+
34+
The `auto` keyword is compatible with various C++ constructs making it a versatile tool in modern C++ programming.
35+
36+
```cpp
37+
auto& element{array[0]}; // reference to an element
38+
const auto object{otherObject}; // const type version of otherObject's type
39+
auto* ptr{&x}; // pointer to x with the same type as x, but as a pointer.
40+
```
41+
42+
In later concept we will often see the `auto` keyword with lambda expressions, range-based for-loops, and iterators.
43+
44+
## Use Cases
45+
46+
- **Generic Programming**: `auto` is particularly useful in generic programming scenarios where types may be complex or template-related.
47+
- **Complex Data Types**: When dealing with complex data types or long type names, `auto` can reduce clutter and improve code maintainability.
48+
- **Iterator-Based Algorithms**: It simplifies the use of iterators in algorithms, enhancing code readability.

concepts/auto/introduction.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Introduction
2+
3+
## The `auto` Keyword in C++
4+
5+
In C++, the `auto` keyword is a powerful feature introduced in C++11, used to declare variables with an inferred data type.
6+
The compiler deduces the type of the variable based on its initializer, which can make code more readable and easier to maintain.
7+
8+
## Example Usage
9+
10+
Consider the following example where `auto` is used to declare variables:
11+
12+
```cpp
13+
auto dragon_population{3}; // dragon_population is deduced as an integer
14+
auto westeros{7.7777}; // westeros is deduced as a double
15+
auto wedding_location{"The Twins"}; // wedding_location is deduced as a const char*, not std::string
16+
```
17+
18+
In each case, the type of the variable is inferred from the value it is initialized with.
19+
20+
## Type Inference
21+
22+
The `auto` keyword helps by writing more concise and readable code by reducing the verbosity of explicit types.
23+
24+
```cpp
25+
const std::vector<std::string> pigeon_pie{"flour", "butter", "pigeon", "salt"};
26+
auto purple_wedding_pie{pigeon_pie};
27+
purple_wedding_pie.emplace_back("the strangler");
28+
```
29+
30+
In this loop, `auto` deduces the type of `purple_wedding_pie` as `std::vector<std::string>`, avoiding the need to explicitly specify the type again.
31+
32+
## Compatibility
33+
34+
The `auto` keyword is compatible with various C++ constructs making it a versatile tool in modern C++ programming.
35+
36+
```cpp
37+
auto& element{array[0]}; // reference to an element
38+
const auto object{otherObject}; // const type version of otherObject's type
39+
auto* ptr{&x}; // pointer to x with the same type as x, but as a pointer.
40+
```
41+
42+
In later concept we will often see the `auto` keyword with lambda expressions, range-based for-loops, and iterators.

concepts/auto/links.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"url": "https://en.cppreference.com/w/cpp/language/auto",
4+
"description": "C++ reference documentation for the auto keyword"
5+
},
6+
{
7+
"url": "https://www.learncpp.com/cpp-tutorial/type-deduction-for-objects-using-the-auto-keyword/",
8+
"description": "Learn C++ overview on the auto keyword and its usage"
9+
}
10+
]

config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,11 @@
12841284
"uuid": "18b2f529-3be6-4a70-be7d-81aa07393585",
12851285
"slug": "functions",
12861286
"name": "Functions"
1287+
},
1288+
{
1289+
"uuid": "da0a69e5-33ec-4458-8c70-80457715ada6",
1290+
"slug": "auto",
1291+
"name": "Auto"
12871292
}
12881293
],
12891294
"key_features": [

0 commit comments

Comments
 (0)