-
-
Notifications
You must be signed in to change notification settings - Fork 234
feat: add auto concept #836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "blurb": "The `auto` keyword in C++ allows automatic type deduction, simplfying code by letting the compiler infer variable types from their initializers.", | ||
| "authors": [ | ||
| "vaeng" | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # About | ||
|
|
||
| ## The `auto` Keyword in C++ | ||
|
|
||
| In C++, the `auto` keyword is a powerful feature introduced in C++11, used to declare variables with an inferred data type. | ||
| The compiler deduces the type of the variable based on its initializer, which can make code more readable and easier to maintain. | ||
|
|
||
| ## Example Usage | ||
|
|
||
| Consider the following example where `auto` is used to declare variables: | ||
|
|
||
| ```cpp | ||
| auto dragon_population{3}; // dragon_population is deduced as an integer | ||
| auto westeros{7.7777}; // westeros is deduced as a double | ||
| auto wedding_location{"The Twins"}; // wedding_location is deduced as a const char*, not std::string | ||
| ``` | ||
|
|
||
| In each case, the type of the variable is inferred from the value it is initialized with. | ||
|
|
||
| ## Type Inference | ||
|
|
||
| The `auto` keyword helps by writing more concise and readable code by reducing the verbosity of explicit types. | ||
|
|
||
| ```cpp | ||
| const std::vector<std::string> pigeon_pie{"flour", "butter", "pigeon", "salt"}; | ||
| auto purple_wedding_pie{pigeon_pie}; | ||
| purple_wedding_pie.emplace_back("the strangler"); | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| ## Compatibility | ||
|
|
||
| The `auto` keyword is compatible with various C++ constructs making it a versatile tool in modern C++ programming. | ||
|
|
||
| ```cpp | ||
| auto& element{array[0]}; // reference to an element | ||
| const auto object{otherObject}; // const type version of otherObject's type | ||
| auto* ptr{&x}; // pointer to x with the same type as x, but as a pointer. | ||
| ``` | ||
|
|
||
| In later concept we will often see the `auto` keyword with lambda expressions, range-based for-loops, and iterators. | ||
|
|
||
| ## Use Cases | ||
|
|
||
| - **Generic Programming**: `auto` is particularly useful in generic programming scenarios where types may be complex or template-related. | ||
| - **Complex Data Types**: When dealing with complex data types or long type names, `auto` can reduce clutter and improve code maintainability. | ||
| - **Iterator-Based Algorithms**: It simplifies the use of iterators in algorithms, enhancing code readability. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # Introduction | ||
|
|
||
| ## The `auto` Keyword in C++ | ||
|
|
||
| In C++, the `auto` keyword is a powerful feature introduced in C++11, used to declare variables with an inferred data type. | ||
| The compiler deduces the type of the variable based on its initializer, which can make code more readable and easier to maintain. | ||
|
|
||
| ## Example Usage | ||
|
|
||
| Consider the following example where `auto` is used to declare variables: | ||
|
|
||
| ```cpp | ||
| auto dragon_population{3}; // dragon_population is deduced as an integer | ||
| auto westeros{7.7777}; // westeros is deduced as a double | ||
| auto wedding_location{"The Twins"}; // wedding_location is deduced as a const char*, not std::string | ||
| ``` | ||
|
|
||
| In each case, the type of the variable is inferred from the value it is initialized with. | ||
|
|
||
| ## Type Inference | ||
|
|
||
| The `auto` keyword helps by writing more concise and readable code by reducing the verbosity of explicit types. | ||
|
|
||
| ```cpp | ||
| const std::vector<std::string> pigeon_pie{"flour", "butter", "pigeon", "salt"}; | ||
| auto purple_wedding_pie{pigeon_pie}; | ||
| purple_wedding_pie.emplace_back("the strangler"); | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| ## Compatibility | ||
|
|
||
| The `auto` keyword is compatible with various C++ constructs making it a versatile tool in modern C++ programming. | ||
|
|
||
| ```cpp | ||
| auto& element{array[0]}; // reference to an element | ||
| const auto object{otherObject}; // const type version of otherObject's type | ||
| auto* ptr{&x}; // pointer to x with the same type as x, but as a pointer. | ||
| ``` | ||
|
|
||
| In later concept we will often see the `auto` keyword with lambda expressions, range-based for-loops, and iterators. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| [ | ||
| { | ||
| "url": "https://en.cppreference.com/w/cpp/language/auto", | ||
| "description": "C++ reference documentation for the auto keyword" | ||
| }, | ||
| { | ||
| "url": "https://www.learncpp.com/cpp-tutorial/type-deduction-for-objects-using-the-auto-keyword/", | ||
| "description": "Learn C++ overview on the auto keyword and its usage" | ||
| } | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! :D