Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions concepts/auto/.meta/config.json
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"
]
}
48 changes: 48 additions & 0 deletions concepts/auto/about.md
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.
42 changes: 42 additions & 0 deletions concepts/auto/introduction.md
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! :D

```

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.
10 changes: 10 additions & 0 deletions concepts/auto/links.json
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"
}
]
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,11 @@
"uuid": "18b2f529-3be6-4a70-be7d-81aa07393585",
"slug": "functions",
"name": "Functions"
},
{
"uuid": "da0a69e5-33ec-4458-8c70-80457715ada6",
"slug": "auto",
"name": "Auto"
}
],
"key_features": [
Expand Down