chore: sync content to repo (#9807)

Co-authored-by: kamranahmedse <4921183+kamranahmedse@users.noreply.github.com>
This commit is contained in:
github-actions[bot]
2026-03-31 21:00:48 +02:00
committed by GitHub
co-authored by kamranahmedse
parent e762acc1c4
commit c0d52ebfe3
123 changed files with 284 additions and 5984 deletions
@@ -1,43 +1,3 @@
# Access Violations
An access violation is a specific type of error that occurs when a program attempts to access an illegal memory location. In C++, access violations are most commonly caused by:
* **Dereferencing a null or invalid pointer.**
* **Accessing an array out of bounds.**
* **Reading or writing to memory freed by the user or the operating system.**
It is crucial to identify access violations because they can lead to unpredictable behavior, application crashes, or corruption of data.
Some examples of access violations are:
Dereferencing null or invalid pointer
-------------------------------------
int *p = nullptr;
int x = *p; // Access violation: trying to access null pointer's content
Accessing an array out of bounds
--------------------------------
int arr[5] = {1, 2, 3, 4, 5};
int y = arr[5]; // Access violation: index out of bounds (valid indices are 0-4)
Reading or writing to freed memory
----------------------------------
int* p2 = new int[10];
delete[] p2;
p2[3] = 42; // Access violation: writing to memory that has been freed
### Debugging Access Violations
Tools like _debuggers_, _static analyzers_, and _profilers_ can help identify access violations in your code. For example:
* **Microsoft Visual Studio**: Use the built-in debugger to identify the line of code responsible for the access violation error.
* **Valgrind**: A popular Linux tool that detects memory leaks and access violations in your C++ programs.
* **AddressSanitizer**: A runtime memory error detector for C++ that can detect out-of-bounds accesses, memory leaks, and use-after-free errors.
An access violation occurs when a program tries to read from or write to a memory location that it doesn't have permission to access. This is a common error in C++ that can arise from issues like dereferencing null or invalid pointers, accessing arrays beyond their bounds, or attempting to use memory that has already been freed. Debugging tools such as debuggers, static analyzers (like those in Visual Studio), and runtime memory error detectors (such as Valgrind or AddressSanitizer) are essential for identifying and resolving these violations by pinpointing the offending code.
@@ -1,91 +1,3 @@
# STL Algorithms
The Standard Template Library (STL) in C++ provides a collection of generic algorithms that are designed to work with various container classes. These algorithms are implemented as functions and can be applied to different data structures, such as arrays, vectors, lists, and others. The primary header file for algorithms is `<algorithm>`.
Key Concepts
------------
Sorting
-------
Sorting refers to arranging a sequence of elements in a specific order. The STL provides several sorting algorithms, such as `std::sort`, `std::stable_sort`, and `std::partial_sort`.
### std::sort
`std::sort` is used to sort a range of elements \[first, last) in non-descending order (by default). You can also use custom comparison functions or lambda expressions to change the sorting order.
Example:
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> nums = {10, 9, 8, 7, 6, 5};
std::sort(nums.begin(), nums.end());
for (int num : nums) {
std::cout << num << ' ';
}
// Output: 5 6 7 8 9 10
}
Searching
---------
Searching refers to finding if a particular element is present within a given range of elements. STL provides various searching algorithms, such as `std::find`, `std::binary_search`, and `std::find_if`.
### std::find
`std::find` is used to find the iterator of the first occurrence of a given value within the range \[first, last).
Example:
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> nums = {5, 6, 7, 8, 9, 10};
auto it = std::find(nums.begin(), nums.end(), 9);
if (it != nums.end()) {
std::cout << "Found 9 at position: " << (it - nums.begin());
} else {
std::cout << "9 not found";
}
// Output: Found 9 at position: 4
}
Modifying Sequences
-------------------
The STL also provides algorithms for modifying sequences, such as `std::remove`, `std::replace`, and `std::unique`.
### std::remove
`std::remove` is used to remove all instances of a value from a container within the given range \[first, last). Note that the function does not resize the container after removing elements.
Example:
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> nums = {5, 6, 7, 6, 8, 6, 9, 6, 10};
nums.erase(std::remove(nums.begin(), nums.end(), 6), nums.end());
for (int num : nums) {
std::cout << num << ' ';
}
// Output: 5 7 8 9 10
}
Summary
-------
STL algorithms in C++ provide a set of useful functions for key operations such as sorting, searching, and modifying sequences. The algorithms can be used with a variety of container classes, making them highly versatile and an essential part of C++ programming.
The C++ Standard Template Library (STL) offers a rich collection of generic algorithms that operate on various container types. These algorithms, found primarily in the `<algorithm>` header, provide functionalities for common tasks like sorting, searching, and sequence manipulation. They promote code reusability and efficiency by working independently of the specific data structures they operate on.
@@ -1,32 +1,3 @@
# Argument Dependent Lookup (ADL)
Argument Dependent Lookup (ADL) or Koenig Lookup is a mechanism in C++ that allows the compiler to search for the appropriate function to call based on the types of arguments provided. It is particularly helpful when using overloaded functions or operators in a namespace.
ADL allows the compiler to find functions in the same namespace as the arguments, even if the function is not declared at the point of use or within the namespace provided. This is especially useful when working with templates or generic programming.
Example
-------
Consider the following example using a namespace and overloaded `operator<<()`:
namespace MyNamespace {
class MyClass {
public:
int value;
};
std::ostream& operator<<(std::ostream& os, const MyClass& obj) {
os << "MyClass: " << obj.value;
return os;
}
}
int main() {
MyNamespace::MyClass obj;
obj.value = 42;
using std::cout; // Required to use 'cout' without fully qualifying it.
cout << obj << '\n'; // ADL is used to find the correct overloaded 'operator<<'.
}
In this example, when you call `cout << obj;` in `main()`, ADL is used to find the correct `operator<<()` in the `MyNamespace` namespace because the argument `obj` is of type `MyNamespace::MyClass`.
Argument Dependent Lookup (ADL), also known as Koenig Lookup, is a feature in C++ that extends the function lookup process by allowing the compiler to search for functions in the namespaces of the function arguments' types. This is especially useful when working with overloaded operators and functions defined within namespaces, enabling more intuitive and concise code by automatically finding the appropriate function based on the argument types, even if it's not explicitly qualified.
@@ -1,66 +1,3 @@
# Arithmetic Operators in C++
# Arithmetic Operators
Arithmetic operators are used to perform mathematical operations with basic variables such as integers and floating-point numbers. Here is a brief summary of the different arithmetic operators in C++:
1\. Addition Operator (`+`)
---------------------------
It adds two numbers together.
int sum = a + b;
2\. Subtraction Operator (`-`)
------------------------------
It subtracts one number from another.
int difference = a - b;
3\. Multiplication Operator (`*`)
---------------------------------
It multiplies two numbers together.
int product = a * b;
4\. Division Operator (`/`)
---------------------------
It divides one number by another. Note that if both operands are integers, it will perform integer division and the result will be an integer.
int quotient = a / b; // integer division
float quotient = float(a) / float(b); // floating-point division
5\. Modulus Operator (`%`)
--------------------------
It calculates the remainder of an integer division.
int remainder = a % b;
6\. Increment Operator (`++`)
-----------------------------
It increments the value of a variable by 1. There are two ways to use this operator: prefix (`++x`) and postfix (`x++`). Prefix increments the value before returning it, whereas postfix returns the value first and then increments it.
int x = 5;
int y = ++x; // x = 6, y = 6
int z = x++; // x = 7, z = 6
7\. Decrement Operator (`--`)
-----------------------------
It decrements the value of a variable by 1. It can also be used in prefix (`--x`) and postfix (`x--`) forms.
int x = 5;
int y = --x; // x = 4, y = 4
int z = x--; // x = 3, z = 4
These are the basic arithmetic operators in C++ that allow you to perform mathematical operations on your variables. Use them in combination with other control structures, such as loops and conditionals, to build more complex programs.
Arithmetic operators are fundamental building blocks in C++ programming, allowing you to perform mathematical calculations directly within your code. These operators work on numeric data types such as integers and floating-point numbers, enabling addition (+), subtraction (-), multiplication (*), division (/), and modulus (%) operations. Furthermore, increment (++) and decrement (--) operators provide concise ways to increase or decrease variable values by one.
@@ -1,45 +1,3 @@
# auto (Automatic Type Deduction)
# Auto Type Deduction
**Auto**
`auto` is a keyword in C++ language introduced in C++11, which is used for automatic type deduction. It automatically deduces the type of a variable from the type of its initializer expression at compile time.
The `auto` keyword is useful when you are dealing with complex types or when the type of a variable is hard to predict. It can help in writing cleaner and less error-prone code.
Here's a simple example of using `auto` for type deduction:
#include <iostream>
#include <vector>
int main() {
// Traditional way of declaring a variable:
int myInt = 5;
// Using auto for type deduction:
auto myAutoInt = 5; // Automatically deduces the type as 'int'
// Example with more complex types:
std::vector<int> myVector = {1, 2, 3, 4, 5};
// Without auto, iterating the vector would look like this:
for (std::vector<int>::iterator it = myVector.begin(); it != myVector.end(); ++it) {
std::cout << *it << '\n';
}
// With auto, the iterator declaration becomes simpler:
for (auto it = myVector.begin(); it != myVector.end(); ++it) {
std::cout << *it << '\n';
}
}
Keep in mind that `auto` deduces the type based on the initializer expression, so if you don't provide an initial value, you will get a compile-time error:
auto myVar; // Error: Cannot deduce the type without initializer
In C++14, you can also use `auto` with function return types to let the compiler automatically deduce the return type based on the returned expression:
auto add(int x, int y) {
return x + y; // The compiler deduces the return type as 'int'
}
`auto` is a keyword in C++ introduced in C++11 that enables automatic type deduction. It allows the compiler to infer the data type of a variable from its initialization expression at compile time. This simplifies code by reducing the need to explicitly declare types, particularly when dealing with complex or less predictable types, leading to cleaner and more maintainable code. In C++14, `auto` can also be used to deduce function return types based on the return expression.
@@ -1,110 +1,3 @@
# Basic Operations in C++
# Basic Operations
Basic operations in C++ refer to the fundamental arithmetic, relational, and logical operations that can be performed using C++ programming language, which are essential for any kind of program or calculation in a real-world scenario.
Here's a summary of the basic operations in C++
Arithmetic Operations
---------------------
These operations are used for performing calculations in C++ and include the following:
* **Addition (+)**: Adds two numbers.
int a = 5;
int b = 6;
int sum = a + b; // sum is 11
* **Subtraction (-)**: Subtracts one number from the other.
int a = 10;
int b = 6;
int diff = a - b; // diff is 4
* **Multiplication (\*)**: Multiplies two numbers.
int a = 3;
int b = 4;
int product = a * b; // product is 12
* **Division (/)**: Divides one number by another, yields quotient.
int a = 12;
int b = 4;
int quotient = a / b; // quotient is 3
* **Modulus (%)**: Divides one number by another, yields remainder.
int a = 15;
int b = 4;
int remainder = a % b; // remainder is 3
Relational Operators
--------------------
These operations compare two values and return a boolean value (true/false) depending on the comparison. The relational operations are:
* **Equal to (==)**: Returns true if both operands are equal.
5 == 5 // true
3 == 4 // false
* **Not equal to (!=)**: Returns true if operands are not equal.
5 != 2 // true
1 != 1 // false
* **Greater than (>)**: Returns true if the first operand is greater than the second.
5 > 3 // true
2 > 3 // false
* **Less than (<)**: Returns true if the first operand is less than the second.
3 < 5 // true
6 < 5 // false
* **Greater than or equal to (>=)**: Returns true if the first operand is greater than or equal to the second.
5 >= 5 // true
6 >= 2 // true
3 >= 4 // false
* **Less than or equal to (<=)**: Returns true if the first operand is less than or equal to the second.
4 <= 4 // true
2 <= 3 // true
5 <= 4 // false
Logical Operators
-----------------
Logical operators are used for combining multiple conditions or boolean values.
* **AND (&&)**: Returns true if both operands are true.
true && true // true
true && false // false
* **OR (||)**: Returns true if any one of the operands is true.
true || false // true
false || false // false
* **NOT (!)**: Returns true if the operand is false and vice versa.
!true // false
!false // true
Understanding fundamental operations is crucial for any C++ developer. This foundational knowledge includes arithmetic operations such as addition, subtraction, multiplication, and division, as well as comparison operations (e.g., equal to, not equal to, greater than) used in conditional logic. Mastering these operations forms the bedrock upon which more complex algorithms and data structures are built.
@@ -1,70 +1,6 @@
# Bitwise Operations
# Bitwise Operators
Bitwise operations are operations that directly manipulate the bits of a number. Bitwise operations are useful for various purposes, such as optimizing algorithms, performing certain calculations, and manipulating memory in lower-level programming languages like C and C++.
Here is a quick summary of common bitwise operations in C++:
Bitwise AND (`&`)
-----------------
The bitwise AND operation (`&`) is a binary operation that takes two numbers, compares them bit by bit, and returns a new number where each bit is set (1) if the corresponding bits in both input numbers are set (1); otherwise, the bit is unset (0).
Example:
int result = 5 & 3; // result will be 1 (0000 0101 & 0000 0011 = 0000 0001)
Bitwise OR (`|`)
----------------
The bitwise OR operation (`|`) is a binary operation that takes two numbers, compares them bit by bit, and returns a new number where each bit is set (1) if at least one of the corresponding bits in either input number is set (1); otherwise, the bit is unset (0).
Example:
int result = 5 | 3; // result will be 7 (0000 0101 | 0000 0011 = 0000 0111)
Bitwise XOR (`^`)
-----------------
The bitwise XOR (exclusive OR) operation (`^`) is a binary operation that takes two numbers, compares them bit by bit, and returns a new number where each bit is set (1) if the corresponding bits in the input numbers are different; otherwise, the bit is unset (0).
Example:
int result = 5 ^ 3; // result will be 6 (0000 0101 ^ 0000 0011 = 0000 0110)
Bitwise NOT (`~`)
-----------------
The bitwise NOT operation (`~`) is a unary operation that takes a single number, and returns a new number where each bit is inverted (1 becomes 0, and 0 becomes 1).
Example:
int result = ~5; // result will be -6 (1111 1010)
Bitwise Left Shift (`<<`)
-------------------------
The bitwise left shift operation (`<<`) is a binary operation that takes two numbers, a value and a shift amount, and returns a new number by shifting the bits of the value to the left by the specified shift amount. The vacated bits are filled with zeros.
Example:
int result = 5 << 1; // result will be 10 (0000 0101 << 1 = 0000 1010)
Bitwise Right Shift (`>>`)
--------------------------
The bitwise right shift operation (`>>`) is a binary operation that takes two numbers, a value and a shift amount, and returns a new number by shifting the bits of the value to the right by the specified shift amount. The vacated bits are filled with zeros or sign bit depending on the input value being signed or unsigned.
Example:
int result = 5 >> 1; // result will be 2 (0000 0101 >> 1 = 0000 0010)
These were the most common bitwise operations in C++. Remember to use them carefully and understand their behavior when applied to specific data types and scenarios.
Bitwise operators in C++ allow direct manipulation of individual bits within integer data types. These operators work by treating values as sequences of bits and performing operations at the bit level. Common bitwise operators include AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). They are utilized for tasks such as setting, clearing, or toggling specific bits, optimizing performance in certain algorithms, and low-level system programming.
Visit the following resources to learn more:
@@ -1 +1,3 @@
# undefined
# Boost
Boost is a set of peer-reviewed, portable C++ source libraries. It provides a wide range of utilities and tools that can significantly enhance your C++ development, covering areas like data structures, algorithms, mathematical functions, concurrency, and more. Boost libraries are designed to integrate well with the C++ Standard Library and often serve as incubators for features that eventually make their way into the standard.
@@ -1,70 +1,3 @@
# Build Systems in C++
# Build Systems
A build system is a collection of tools and utilities that automate the process of compiling, linking, and executing source code files in a project. The primary goal of build systems is to manage the complexity of the compilation process and produce a build (executable or binary files) in the end. In C++ (cpp), some common build systems are:
* **GNU Make**: It is a popular build system that uses `Makefile` to define the build process. It checks the dependencies and timestamps of source files to determine which files need to be compiled and linked.
Code example:
# Makefile
CXX = g++
CPPFLAGS = -Wall -std=c++11
TARGET = HelloWorld
all: $(TARGET)
$(TARGET): main.cpp
$(CXX) $(CPPFLAGS)main.cpp -o $(TARGET)
clean:
rm $(TARGET)
* **CMake**: It is a cross-platform build system that focuses on defining project dependencies and managing build environments. CMake generates build files (like Makefiles) for different platforms and allows developers to write source code once and then compile it for different target platforms.
Code example:
# CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(HelloWorld)
set(CMAKE_CXX_STANDARD 11)
add_executable(HelloWorld main.cpp)
* **Autotools**: Also known as GNU Build System, consists of the GNU Autoconf, Automake, and Libtool tools that enable developers to create portable software across different Unix-based systems. For a C++ project, you will need to create `configure.ac`, `Makefile.am` files with specific rules, and then run the following commands in the terminal to build the project:
autoreconf --install
./configure
make
make install
* **SCons**: This build system uses Python for build scripts, making it more expressive than GNU Make. It can also build for multiple platforms and configurations simultaneously.
Code example:
# SConstruct
env = Environment()
env.Program(target="HelloWorld", source=["main.cpp"])
* **Ninja**: A small and focused build system that takes a list of build targets specified in a human-readable text file and builds them as fast as possible.
Code example:
# build.ninja
rule cc
command = g++ -c $in -o $out
rule link
command = g++ $in -o $out
build main.o: cc main.cpp
build HelloWorld: link main.o
default HelloWorld
These are some of the popular build systems in C++, each with their own syntax and capabilities. While Make is widely used, CMake is a cross-platform build system that generates build files for other build systems like Make or Ninja. Autotools is suitable for creating portable software, SCons leverages Python for its build scripts, and Ninja focuses on fast build times.
Build systems in C++ automate the compilation, linking, and execution of source code, managing the complexity of the build process to produce executables or libraries. Common examples include GNU Make, which uses Makefiles to track dependencies and timestamps, and CMake, a cross-platform system that generates build files for various platforms. Autotools is another option for creating portable software across different Unix-based systems. SCons leverages Python for more expressive build scripts, while Ninja focuses on speed by efficiently building targets specified in a simple text file. Each system offers different approaches to streamlining the software construction process.
@@ -1,67 +1,3 @@
# C++0x
# C++11
`cpp0x` refers to the working name for [C++11](https://en.cppreference.com/w/cpp/11), which was previously known as C++0x before its final release. C++11 is a major revision of the C++ language standard, published in 2011, and brought several new features and improvements to the language.
Some of the notable features in C++11 include:
* **Auto** keyword for automatic type inference.
auto i = 42; // i is an int
auto s = "hello"; // s is a const char*
* **Range-based for loop** for easier iteration over containers.
std::vector<int> vec = {1, 2, 3};
for (int i : vec) {
std::cout << i << '\n';
}
* **Lambda functions** for creating anonymous functions.
auto add = [](int a, int b) { return a + b; };
int result = add(3, 4); // result is 7
* **nullptr** for representing null pointer values, instead of using `NULL`.
int* p = nullptr;
* **Rvalue references and move semantics** to optimize the handling of temporary objects.
std::string str1 = "hello";
std::string str2 = std::move(str1); // move the content of str1 to str2
* **Variadic templates** for creating templates that take a variable number of arguments.
template <typename... Args>
void printArgs(Args... args) {
// function body
}
* **Static assertions** for compile-time assertions.
static_assert(sizeof(int) == 4, "This code requires int to be 4 bytes.");
* **Thread support** for multithreading programming.
#include <thread>
void my_function() {
// thread function body
}
int main() {
std::thread t(my_function);
t.join();
return 0;
}
These are just a few examples of the many new features introduced in C++11. For a comprehensive list, you can refer to the [C++11 documentation](https://en.cppreference.com/w/cpp/11).
C++11, formerly known as C++0x, represents a significant evolution of the C++ language standard, finalized in 2011. This version introduced features like automatic type inference with `auto`, simplified container iteration using range-based for loops, the ability to create anonymous functions via lambda expressions, and a safer way to represent null pointers using `nullptr`. Furthermore, C++11 optimized temporary object handling through rvalue references and move semantics, enabled templates to accept a variable number of arguments with variadic templates, facilitated compile-time assertions via static assertions, and integrated thread support for concurrent programming.
@@ -1,62 +1,3 @@
# C++11 and C++14
**C++11** The C++11 standard, also known as C++0x, was officially released in September 2011. It introduced several new language features and improvements, including:
* **Auto**: Allows compiler to infer the variable type based on its initializing expression.
auto integer = 42; // integer is of int type
auto floating = 3.14; // floating is of double type
* **Range-Based for Loop**: Provides foreach-like semantics for iterating through a container or array.
std::vector<int> numbers {1, 2, 3, 4};
for (int number : numbers) {
std::cout << number << '\n';
}
* **Lambda Functions**: Anonymous functions that allow the creation of function objects more easily.
auto add = [](int a, int b) -> int { return a + b; };
int sum = add(42, 13); // sum is equal to 55
* **nullptr**: A new keyword to represent null pointers, more type-safe than using a literal '0' or "NULL".
int *ptr = nullptr;
* **Thread Support Library**: Provides a standard way to work with threads and synchronize data access across threads.
std::thread t([]() { std::cout << "Hello from another thread\n"; });
t.join();
**C++14** The C++14 standard was officially released in December 2014 as a small extension over C++11, focusing more on fine-tuning language features and fixing issues. Some of the new features introduced:
* **Generic Lambdas**: Allows lambda function parameters to be declared with 'auto' type placeholders.
auto add = [](auto a, auto b) { return a + b; };
auto sum_i = add(42, 13); // Still works with integers
auto sum_f = add(3.14, 2.72); // Now works with doubles too
* **Binary Literals**: Allow you to input integers as binary literals for better readability.
int b = 0b110101; // Decimal value is 53
* **decltype(auto)**: Deduces the type of variable to match that of the expression it is initialized with.
auto func = [](auto a, auto b) { return a * b; };
decltype(auto) result = func(5, 3.14); // decltype(auto) deduces to "double"
* **Variable Templates**: Allows you to define variables with template parameters.
template <typename T>
constexpr T pi = T(3.1415926535897932385);
float r = pi<float>; // Instantiated as a float
double d = pi<double>; // Instantiated as a double
C++11 and C++14 are significant updates to the C++ language, introducing features to enhance its power and usability. C++11 brought in features like `auto` for type inference, range-based for loops for simpler iteration, lambda functions for creating anonymous functions, `nullptr` for safer null pointer representation, and a standard thread support library. Building upon this, C++14 refined these features and added new capabilities, including generic lambdas, binary literals, `decltype(auto)` for precise type deduction, and variable templates. These standards made C++ more modern, expressive, and efficient.
@@ -1,64 +1,3 @@
# C++17
C++17, also known as C++1z, is the version of the C++ programming language published in December 2017. It builds upon the previous standard, C++14, and adds various new features and enhancements to improve the language's expressiveness, performance, and usability.
Key Features:
-------------
* If-init-statement: Introduces a new syntax for writing conditions with scope inside if and switch statements.
if (auto it = map.find(key); it != map.end())
{
// Use it
}
* Structured Binding Declarations: Simplify the process of unpacking a tuple, pair, or other aggregate types.
map<string, int> data;
auto [iter, success] = data.emplace("example", 42);
* Inline variables: Enables `inline` keyword for variables and allows single definition of global and class static variables in header files.
inline int globalVar = 0;
* Folds expressions: Introduce fold expressions for variadic templates.
template <typename... Ts>
auto sum(Ts... ts)
{
return (ts + ...);
}
* constexpr if statement: Allows conditional compilation during compile time.
template <typename T>
auto get_value(T t)
{
if constexpr (std::is_pointer_v<T>)
{
return *t;
}
else
{
return t;
}
}
* Improved lambda expression: Allows lambda to capture a single object without changing its type or constness.
auto func = [x = std::move(obj)] { /* use x */ };
* Standard file system library: `std::filesystem` as a standardized way to manipulate paths, directories, and files.
* New Standard Library additions: `<string_view>` (non-owning string reference), `<any>` (type-erased container), `<optional>` (optional value wrapper), `<variant>` (type-safe discriminated union / sum type), and `<memory_resource>` (library for polymorphic allocators).
* Parallel Algorithms: Adds support for parallel execution of Standard Library algorithms.
This is a brief summary of the key features of C++17; it includes more features and library updates. For a complete list, you can refer to the [full list of C++17 features and changes](https://en.cppreference.com/w/cpp/17).
C++17 is a significant update to the C++ language, offering new features and enhancements that improve code expressiveness and efficiency. This version introduces features such as structured bindings for easier data unpacking, `if` and `constexpr if` statements for compile-time conditional logic, and inline variables for simplified header usage. Furthermore, C++17 incorporates the `std::filesystem` library for file system operations, as well as `std::string_view`, `std::any`, `std::optional`, and `std::variant` for enhanced data handling, alongside parallel algorithms to boost performance.
@@ -1,84 +1,3 @@
# C++20
C++20 is the latest standard of the C++ programming language, which brings significant improvements and new features to the language. This version is aimed at facilitating better software development practices and enabling developers to write more efficient, readable, and maintainable code.
Here are some of the key features introduced in C++20:
Concepts
--------
Concepts are a way to enforce specific requirements on template parameters, allowing you to write more expressive and understandable code. They improve the error messages when using templates and ensure that the template parameters fulfill specific criteria.
template <typename T>
concept Addable = requires (T a, T b) {
{ a + b } -> std::same_as<T>;
};
template <Addable T>
T add(T a, T b) {
return a + b;
}
Ranges
------
Ranges provide a new way to work with sequences of values, enhancing the power and expressiveness of the Standard Library algorithms. The range-based algorithms make it easier and more convenient to work with sequences.
#include <algorithm>
#include <iostream>
#include <ranges>
#include <vector>
int main() {
std::vector<int> numbers = { 1, 2, 3, 4, 5 };
auto even_numbers = numbers | std::views::filter([](int n) { return n % 2 == 0; });
for (int n : even_numbers) {
std::cout << n << ' ';
}
}
Coroutines
----------
Coroutines are a new way to write asynchronous and concurrent code with improved readability. They allow functions to be suspended and resumed, enabling you to write more efficient, non-blocking code.
#include <coroutine>
#include <iostream>
#include <future>
std::future<int> async_value(int value) {
co_await std::chrono::seconds(1);
co_return value * 2;
}
int main() {
auto result = async_value(42);
std::cout << "Result: " << result.get() << '\n';
}
The `constexpr` and `consteval` Keywords
----------------------------------------
Both `constexpr` and `consteval` are related to compile-time evaluation. Functions marked with `constexpr` can be executed at compile-time or runtime, while functions marked with `consteval` can only be executed at compile-time.
constexpr int add(int a, int b) {
return a + b;
}
consteval int square(int x) {
return x * x;
}
int main() {
constexpr int result1 = add(3, 4); // evaluated at compile-time
int result2 = add(5, 6); // evaluated at runtime
constexpr int result3 = square(7); // evaluated at compile-time
}
These are just some of the highlights of the C++20 standard. It also includes many other features and improvements, like structured bindings, improved lambdas, and new standard library components. Overall, C++20 makes it easier for developers to write clean, efficient, and expressive code.
C++20 represents a significant evolution of the C++ language, introducing key features aimed at improving code clarity, efficiency, and concurrency. Highlights include Concepts, which enforce constraints on template parameters for better error messages; Ranges, a new way to work with sequences of values enhancing standard library algorithms; Coroutines, for writing asynchronous code with improved readability; and enhancements to compile-time evaluation with `constexpr` and `consteval`. These additions, along with other library improvements, empower developers to create more expressive, robust, and high-performance applications.
@@ -0,0 +1,10 @@
# C++23
C++23 is the latest version of the C++ standard, building upon previous iterations to introduce new features, library enhancements, and language improvements. It aims to simplify development, improve performance, and provide more modern tools for C++ programmers. This standard incorporates features like `std::expected`, stackable coroutines, and improvements to the standard library, making C++ more robust and expressive.
Visit the following resources to learn more:
- [@official@The Standard](https://isocpp.org/std/the-standard)
- [@article@C++23](https://en.wikipedia.org/wiki/C%2B%2B23)
- [@article@Overview of New Features in C++23](https://medium.com/@threehappyer/overview-of-new-features-in-c-23-68c5bc668958)
- [@video@C++23: An Overview of Almost All New and Updated Features - Marc Gregoire - CppCon 2023](https://www.youtube.com/watch?v=Cttb8vMuq-Y)
@@ -1,128 +1,7 @@
# C vs C++
C and C++ are two popular programming languages with some similarities, but they also have key differences. C++ is an extension of the C programming language, with added features such as object-oriented programming, classes, and exception handling. Although both languages are used for similar tasks, they have their own syntax and semantics, which makes them distinct from each other.
C and C++ are both programming languages, with C++ evolving from C. C is a procedural language focused on structured programming using functions. C++ expands upon C by adding object-oriented programming (OOP) features like classes, inheritance, and polymorphism, enabling more complex and modular software design. C++ also introduces features like operator overloading, templates, and exception handling, offering greater flexibility and abstraction compared to C.
Syntax and Semantics
--------------------
Visit the following resources to learn more:
### C
* C is a procedural programming language.
* Focuses on functions and structured programming.
* Does not support objects or classes.
* Memory management is manual, using functions like `malloc` and `free`.
Here's an example of manual memory management in C:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[20];
int score;
} Player;
int main() {
// 1. Allocate: malloc needs the exact byte size
Player* p = (Player*)malloc(sizeof(Player));
if (p != NULL) {
// 2. Initialize: You MUST do this manually
strcpy(p->name, "AdrianMtzTrev");
p->score = 0;
printf("Player %s joined with score %d\n", p->name, p->score);
// 3. Free: Just drops the memory
free(p);
}
return 0;
}
```
### C++
* C++ is both procedural and object-oriented.
* Supports both functions and classes.
* Incorporates different programming paradigms.
* Memory management can be manual (like C) or rely on constructors/destructors and smart pointers.
Here's the equivalent example using C++ classes:
```cpp
#include <iostream>
#include <string>
class Player {
public:
std::string name;
int score;
// Constructor: Runs automatically during 'new'
Player(std::string n) : name(n), score(0) {
std::cout << name << " enters the game!\n";
}
// Destructor: Runs automatically during 'delete'
~Player() {
std::cout << name << " has left the game.\n";
}
};
int main() {
// 1. Create: Allocates AND initializes in one step
Player* p = new Player("Elizabeth");
std::cout << "Current score: " << p->score << std::endl;
// 2. Destroy: Runs the destructor first, then releases memory
delete p;
return 0;
}
```
With **smart pointers**
```cpp
int main() {
// The "Modern" C++ way - no 'delete' required!
auto p = std::make_unique<Player>("Ricardo");
std::cout << "Current score: " << p->score << std::endl;
} // p is automatically destroyed here
```
Code Reusability and Modularity
-------------------------------
### C
* Code reusability is achieved through functions and modular programming.
* High cohesion and low coupling are achieved via structured design.
* Function libraries can be created and included through headers.
### C++
* Offers better code reusability with classes, inheritance, and polymorphism.
* Code modularity is enhanced through namespaces and well-designed object-oriented hierarchy.
Error Handling
--------------
### C
* Error handling in C is done primarily through return codes.
* Lacks support for exceptions or any built-in error handling mechanism.
### C++
* Offers exception handling, which can be used to handle errors that may occur during program execution.
* Enables catching and handling exceptions with `try`, `catch`, and `throw` keywords, providing more control over error handling.
Conclusion
----------
Both C and C++ are powerful languages with unique features and capabilities. While C is simpler and focuses on procedural programming, C++ offers the versatility of using different programming paradigms and improved code organization. Understanding the differences between these two languages can help you decide which one is more suitable for your specific needs and programming style.
- [@article@C++ vs. C: When (and when not) to use each language](https://roadmap.sh/cpp/vs-c)
@@ -1 +1,3 @@
# undefined
# Catch2
Catch2 is a modern, C++-native, header-only framework for writing unit tests, benchmarks, and even simple command-line applications. It provides a simple and expressive syntax for defining test cases, assertions, and sections, making it easy to write and maintain comprehensive tests for your C++ code. It supports features like test case discovery, parameterized tests, and integration with various IDEs and build systems.
@@ -1,56 +1,3 @@
# CMake
CMake is a powerful cross-platform build system that generates build files, Makefiles, or workspaces for various platforms and compilers. Unlike the others build systems, CMake does not actually build the project, it only generates the files needed by build tools. CMake is widely used, particularly in C++ projects, for its ease of use and flexibility.
CMakeLists.txt
--------------
CMake uses a file called `CMakeLists.txt` to define settings, source files, libraries, and other configurations. A typical `CMakeLists.txt` for a simple project would look like:
cmake_minimum_required(VERSION 3.0)
project(MyProject)
set(SRC_DIR "${CMAKE_CURRENT_LIST_DIR}/src")
set(SOURCES "${SRC_DIR}/main.cpp" "${SRC_DIR}/file1.cpp" "${SRC_DIR}/file2.cpp")
add_executable(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_LIST_DIR}/include")
set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_STANDARD 14
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
)
Building with CMake
-------------------
Here is an example of a simple build process using CMake:
* Create a new directory for the build.
mkdir build
cd build
* Generate build files using CMake.
cmake ..
In this example, `..` indicates the parent directory where `CMakeLists.txt` is located. The build files will be generated in the `build` directory.
* Build the project using the generated build files.
make
Or, on Windows with Visual Studio, you may use:
msbuild MyProject.sln
CMake makes it easy to manage large projects, define custom build configurations, and work with many different compilers and operating systems. Making it a widely chosen tool for managing build systems in C++ projects.
CMake is a cross-platform build system generator. It uses configuration files (CMakeLists.txt) to describe the build process and generates native build files like Makefiles or project files for IDEs like Visual Studio. CMake simplifies the build process, especially for complex projects, by abstracting the underlying build tools and providing a consistent interface across different platforms and compilers. It manages dependencies, specifies compiler flags, and handles the overall structure of your project's build process without actually building the project itself.
@@ -1,19 +1,6 @@
# Code Editors
# Code Editors / IDEs
Code editors and IDEs are programs specifically designed for editing, managing and writing source code. They offer a wide range of features that make the development process easier and faster. Here's a brief introduction to some of the most popular code editors and IDEs for C++:
* **Visual Studio**: Visual Studio is an Integrated Development Environment (IDE) for Windows, developed by Microsoft. It includes its own integrated compiler known as Microsoft Visual C++ (MSVC).
* **Visual Studio Code (VSCode)**: Visual Studio Code is a popular, free, open-source, and lightweight code editor developed by Microsoft. It offers an extensive library of extensions that enhance functionality for C++ development.
* **Sublime Text**: Sublime Text is a cross-platform text editor that is quite popular among developers due to its speed and minimalist design. It supports C++ with the help of plugins and has a variety of themes and packages available for customization.
* **CLion**: CLion is an Integrated Development Environment (IDE) developed by JetBrains specifically for C and C++ developers. It provides advanced features like code completion, refactoring support, debugging, and more. It's worth noting that CLion is a commercial IDE, but there is community version available.
These are just a few examples, and there are many other code editors available, including Atom, Notepad++, and Geany. They all have their features and may suit different developers' needs. Finding the right code editor is often a matter of personal preference and workflow.
To work with C++ in your chosen code editor, you often need to install some additional tools and add-ons, such as compilers, linters, and debugger support. Make sure to follow the instructions provided in the editor's documentation to set up C++ correctly.
Choosing the right code editor or IDE is a crucial first step for any C++ developer. These tools provide a platform for writing, editing, and managing your code, often including features like syntax highlighting, code completion, debugging tools, and build automation. Popular options range from lightweight and customizable editors like Visual Studio Code and Sublime Text, to more comprehensive IDEs like Visual Studio and CLion, each with its own strengths and features. Selecting the best one for you often comes down to personal preference and your specific development needs.
Visit the following resources to learn more:
@@ -1,58 +1,3 @@
# Stages of Compilation in C++
# Compiler Stages
The process of compilation in C++ can be divided into four primary stages: Preprocessing, Compilation, Assembly, and Linking. Each stage performs a specific task, ultimately converting the source code into an executable program.
Preprocessing
-------------
The first stage is the preprocessing of the source code. Preprocessors modify the source code before the actual compilation process. They handle directives that start with a `#` (hash) symbol, like `#include`, `#define`, and `#if`. In this stage, included header files are expanded, macros are replaced, and conditional compilation statements are processed.
**Code Example:**
#include <iostream>
#define PI 3.14
int main() {
std::cout << "The value of PI is: " << PI << '\n';
return 0;
}
Compilation
-----------
The second stage is the actual compilation of the preprocessed source code. The compiler translates the modified source code into an intermediate representation, usually specific to the target processor architecture. This step also involves performing syntax checking, semantic analysis, and producing error messages for any issues encountered in the source code.
**Code Example:**
int main() {
int a = 10;
int b = 20;
int sum = a + b;
return 0;
}
Assembly
--------
The third stage is converting the compiler's intermediate representation into assembly language. This stage generates assembly code using mnemonics and syntax that is specific to the target processor architecture. Assemblers then convert this assembly code into object code (machine code).
**Code Example (x86 Assembly):**
mov eax, 10
mov ebx, 20
add eax, ebx
Linking
-------
The final stage is the linking of the object code with the necessary libraries and other object files. In this stage, the linker merges multiple object files and libraries, resolves external references from other modules or libraries, allocates memory addresses for functions and variables, and generates an executable file that can be run on the target platform.
**Code Example (linking objects and libraries):**
$ g++ main.o -o main -lm
In summary, the compilation process in C++ involves four primary stages: preprocessing, compilation, assembly, and linking. Each stage plays a crucial role in transforming the source code into an executable program.
The compilation process in C++ transforms human-readable source code into an executable program through four distinct stages. These stages are: Preprocessing, which handles directives like `#include` and macro replacements; Compilation, where the preprocessed code is translated into assembly language; Assembly, which converts the assembly code into object code; and Linking, where object code is combined with libraries to create the final executable. Each stage performs a specific set of tasks to ensure the code is properly transformed for execution on the target platform.
@@ -1,16 +1,3 @@
# Features of C++ Compilers
# Compilers and Features
Different C++ compilers have different features. Some of the most common features of C++ compilers are:
* **Optimization:** Compilers can optimize the code to improve the performance of the program. For example, they can remove redundant code, inline functions, and perform loop unrolling.
* **Debugging:** Compilers can generate debugging information that can be used to debug the program.
* **Warnings:** Compilers can generate warnings for suspicious code that may cause errors.
Some of the most popular C++ compilers are:
* **GNU Compiler Collection (GCC):** GCC is a free and open-source compiler that supports many programming languages, including C++.
* **Clang:** Clang is a C++ compiler that is part of the LLVM project. It is designed to be compatible with GCC.
* **Microsoft Visual C++:** Microsoft Visual C++ is a C++ compiler that is part of the Microsoft Visual Studio IDE.
* **Intel C++ Compiler:** Intel C++ Compiler is a C++ compiler that is part of the Intel Parallel Studio XE suite.
You should go through the documentation of your compiler to learn more about its features.
C++ compilers translate human-readable C++ code into machine-executable code. They offer various features, including optimization techniques to improve program performance by eliminating redundancies and restructuring code, debugging information generation to aid in troubleshooting, and warning systems to flag potentially problematic code. Popular compilers include GCC, a versatile open-source option; Clang, known for its compatibility and diagnostics; Microsoft Visual C++, integrated within the Visual Studio IDE; and the Intel C++ Compiler, part of the Intel Parallel Studio XE suite. Familiarizing yourself with your chosen compiler's documentation is essential to fully leverage its capabilities.
@@ -1,42 +1,3 @@
# Compilers
A compiler is a computer program that translates source code written in one programming language into a different language, usually machine code or assembly code, that can be executed directly by a computer's processor. In the context of C++, compilers take your written C++ source code and convert it into an executable program.
Popular C++ Compilers
---------------------
There are several popular C++ compilers available, here's a short list of some common ones:
* **GNU Compiler Collection (GCC)**: Developed by the GNU Project, GCC is an open-source compiler that supports multiple programming languages, including C++.
* **Clang**: As part of the LLVM project, Clang is another open-source compiler that supports C++ and is known for its fast compilation times and extensive diagnostics.
* **Microsoft Visual C++ (MSVC)**: MSVC is a commercial compiler provided by Microsoft as part of Visual Studio, and it's widely used on Windows platforms.
* **Intel C++ Compiler (ICC)**: ICC is a commercial compiler provided by Intel and is known for its ability to optimize code for the latest Intel processors.
Example of a Simple C++ Compilation
-----------------------------------
Let's say you have a simple C++ program saved in a file called `hello.cpp`:
#include <iostream>
int main() {
std::cout << "Hello, World!\n";
return 0;
}
You can compile this program using the GCC compiler by executing the following command in a command-line/terminal:
g++ hello.cpp -o hello
This will generate an executable file called `hello` (or `hello.exe` on Windows) which you can run to see the output "Hello, World!".
Note
----
When learning about compilers, it's essential to know that they work closely with the linker and the standard library. The linker takes care of combining compiled object files and libraries into a single executable, while the standard library provides implementations for common functionalities used in your code.
A compiler is a program that translates source code from one programming language into another, often machine code, enabling a computer to execute the instructions. In C++, compilers take your human-readable code and convert it into an executable program. Popular options include GCC, Clang, MSVC, and ICC, each with its own strengths in terms of platform support, optimization, and diagnostics. Compilers work with linkers and standard libraries to produce the final executable, combining compiled code and providing common functionalities.
@@ -1,77 +1,3 @@
# Conan
[Conan](https://conan.io/) is a popular package manager for C and C++ languages and is designed to be cross-platform, extensible, and easy to use. It allows developers to declare, manage, and fetch dependencies while automating the build process. Conan supports various build systems, such as CMake, Visual Studio, MSBuild, and more.
Installation
------------
To install Conan, you can use pip, the Python package manager:
pip install conan
Basic Usage
-----------
* Create a `conanfile.txt` file in your project root directory, specifying dependencies you need for your project:
[requires]
boost/1.75.0
[generators]
cmake
* Run the `conan install` command to fetch and build required dependencies:
mkdir build && cd build
conan install ..
* Now build your project using your build system, for example CMake:
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build .
Creating Packages
-----------------
To create a package in Conan, you need to write a `conanfile.py` file with package information and build instructions.
Here's an example:
from conans import ConanFile, CMake
class MyLibraryConan(ConanFile):
name = "MyLibrary"
version = "0.1"
license = "MIT"
url = "https://github.com/username/mylibrary"
description = "A simple example library"
settings = "os", "compiler", "build_type", "arch"
generators = "cmake"
def build(self):
cmake = CMake(self)
cmake.configure(source_folder="src")
cmake.build()
def package(self):
self.copy("*.hpp", dst="include", src="src/include")
self.copy("*.lib", dst="lib", keep_path=False)
self.copy("*.dll", dst="bin", keep_path=False)
self.copy("*.so", dst="lib", keep_path=False)
self.copy("*.a", dst="lib", keep_path=False)
def package_info(self):
self.cpp_info.libs = ["MyLibrary"]
With that setup, you can create a package by running:
conan create . username/channel
This will compile the package and store it in your Conan cache. You can now use this package as a dependency in other projects.
Conan is a package manager specifically designed for C and C++ projects. It helps developers manage dependencies, build configurations, and share pre-built libraries across different platforms and build systems like CMake and Visual Studio. Using Conan, you can easily declare project dependencies, fetch them from remote repositories, and integrate them into your build process, simplifying the overall dependency management workflow.
@@ -1,36 +1,3 @@
# const_cast
`const_cast` is a type of casting in C++ that allows you to remove or add constness to a variable. In other words, it enables you to modify a `const` or `volatile` object, or change a pointer or reference to a `const` or `volatile` type. This is useful in certain scenarios when you need to pass a `const` variable as an argument or when a function parameter requires a non-const type, but you want to make sure the variable remains constant throughout the code.
Keep in mind that using `const_cast` to modify a truly `const` variable can lead to undefined behavior, so it is best to use this feature only when absolutely necessary.
Example
-------
Here's a code example showing how to use `const_cast`:
#include <cassert>
#include <iostream>
void modifyVariable(int* ptr) {
*ptr = 42;
}
int main() {
const int original_value = 10;
int* non_const_value_ptr = const_cast<int*>(&original_value);
std::cout << "Original value: " << original_value << '\n';
modifyVariable(non_const_value_ptr);
std::cout << "Modified value: " << *non_const_value_ptr << ", original_value: " << original_value << '\n';
assert(non_const_value_ptr == &original_value);
return 0;
}
In this example, we first create a `const` variable, `original_value`. Then we use `const_cast` to remove the constness of the variable and assign it to a non-const pointer, `non_const_value_ptr`. The `modifyVariable` function takes an `int*` as an argument and modifies the value pointed to by the pointer, which would not have been possible if we passed the original `const int` directly. Finally, we print the `original_value` and the `*non_const_value_ptr`, which shows that the value has been modified using `const_cast`.
Please note that this example comes with some risks, as it touches undefined behavior. \*/
`const_cast` is a C++ casting operator that allows you to explicitly add or remove the `const` or `volatile` qualifier from a variable's type. This essentially enables you to modify an object that was initially declared as `const` or pass a `const` object to a function that expects a non-`const` argument. It's a powerful tool but should be used with caution, as modifying a truly `const` object directly can lead to undefined behavior.
@@ -1 +1,3 @@
# Control Flow & Statements
# Control Flow & Statements
Control flow in C++ dictates the order in which statements are executed. It allows programs to make decisions, repeat actions, and execute different code blocks based on conditions. Statements like `if`, `else if`, and `else` enable conditional execution, while loops such as `for`, `while`, and `do-while` facilitate repetition. `switch` statements offer a multi-way branching mechanism based on the value of an expression. These constructs are fundamental for creating programs that respond dynamically to input and perform complex tasks.
@@ -1,37 +1,3 @@
# Copy and Swap
Copy-swap is a C++ idiom that leverages the copy constructor and swap function to create an assignment operator. It follows a simple, yet powerful paradigm: create a temporary copy of the right-hand side object, and swap its contents with the left-hand side object.
Here's a brief summary:
* **Copy**: Create a local copy of the right-hand side object. This step leverages the copy constructor, providing exception safety and code reuse.
* **Swap**: Swap the contents of the left-hand side object with the temporary copy. This step typically involves swapping internal pointers or resources, without needing to copy the full contents again.
* **Destruction**: Destroy the temporary copy. This happens upon the exit of the assignment operator.
Here's a code example for a simple `String` class:
class String {
// ... rest of the class ...
String(const String& other);
friend void swap(String& first, String& second) {
using std::swap; // for arguments-dependent lookup (ADL)
swap(first.size_, second.size_);
swap(first.buffer_, second.buffer_);
}
String& operator=(String other) {
swap(*this, other);
return *this;
}
};
Using the copy-swap idiom:
* The right-hand side object is copied when passed by value to the assignment operator.
* The left-hand side object's contents are swapped with the temporary copy.
* The temporary copy is destroyed, releasing any resources that were previously held by the left-hand side object.
This approach simplifies the implementation and provides strong exception safety, while reusing the copy constructor and destructor code.
Copy and Swap is a C++ idiom used to implement the assignment operator in a safe and efficient manner. It works by creating a copy of the right-hand side object, then swapping the internal state of the copy with the object being assigned to. The temporary copy, now holding the original state, is then destroyed when the function exits, ensuring proper resource management and exception safety. This leverages existing copy constructors and swap functions, leading to cleaner and more robust code.
@@ -1,42 +1,3 @@
# Copy-Write Idiom
# Copy on Write
The Copy-Write idiom, sometimes called the Copy-on-Write (CoW) or "lazy copying" idiom, is a technique used in programming to minimize the overhead of copying large objects. It helps in reducing the number of actual copy operations by using shared references to objects and only copying the data when it's required for modification.
Let's understand this with a simple example:
#include <iostream>
#include <memory>
class MyString {
public:
MyString(const std::string &str) : data(std::make_shared<std::string>(str)) {}
// Use the same shared data for copying.
MyString(const MyString &other) : data(other.data) {
std::cout << "Copied using the Copy-Write idiom.\n";
}
// Make a copy only if we want to modify the data.
void write(const std::string &str) {
// Check if there's more than one reference.
if (data.use_count() > 1) {
data = std::make_shared<std::string>(*data);
std::cout << "Copy is actually made for writing.\n";
}
*data = str;
}
private:
std::shared_ptr<std::string> data;
};
int main() {
MyString str1("Hello");
MyString str2 = str1; // No copy operation, just shared references.
str1.write("Hello, World!"); // This is where the actual duplication happens.
return 0;
}
In this example, we have a class `MyString` that simulates the Copy-Write idiom. When a `MyString` object is created, it constructs a `shared_ptr` pointing to a string. When a `MyString` object is copied, it does not perform any actual copy operation, but simply increases the reference count of the shared object. Finally, when the `write` function is called, it checks if there's more than one reference to the data and if so, it actually creates a new copy and updates the reference. This way, unnecessary copies can be avoided until they are actually needed for modification.
The Copy-on-Write (CoW) idiom is an optimization technique used to defer or eliminate the cost of copying resources until the first write operation. Instead of creating a new copy of an object immediately, the original object and the "copy" share the same underlying resource. Only when one of the objects attempts to modify the shared resource is a true copy created. This can significantly improve performance by avoiding unnecessary copying, especially for large objects.
@@ -1,47 +1,3 @@
# CRTP
**Curiously Recurring Template Pattern (CRTP)**
The Curiously Recurring Template Pattern (CRTP) is a C++ idiom that involves a class template being derived from its own specialization. This pattern allows for the creation of static polymorphism, which differs from regular runtime polymorphism that relies on virtual functions and inheritance.
CRTP is usually employed when you want to customize certain behavior in the base class without adding the overhead of a virtual function call. In short, CRTP can be used for achieving compile-time polymorphism without the runtime performance cost.
Here's an example demonstrating CRTP:
template <typename Derived>
class Base {
public:
void interface() {
static_cast<Derived*>(this)->implementation();
}
void implementation() {
std::cout << "Default implementation in Base\n";
}
};
class Derived1 : public Base<Derived1> {
public:
void implementation() {
std::cout << "Custom implementation in Derived1\n";
}
};
class Derived2 : public Base<Derived2> {
// No custom implementation, so Base::implementation will be used.
};
int main() {
Derived1 d1;
d1.interface(); // Output: "Custom implementation in Derived1"
Derived2 d2;
d2.interface(); // Output: "Default implementation in Base"
return 0;
}
In this example, the `Base` class is a template that takes a single type parameter. `Derived1` and `Derived2` are derived from their respective specialization of `Base`. CRTP is employed to allow custom implementations of the `implementation()` function in derived classes while providing a default behavior in the `Base` class. The `interface()` function in the `Base` class is a template for the derived class's behavior and calls the corresponding `implementation()` function based on the static type.
This pattern enables you to override certain behavior in derived classes with additional functionality, all while avoiding the overhead of virtual function calls and, in turn, achieving a higher degree of efficiency at runtime.
The Curiously Recurring Template Pattern (CRTP) is a C++ idiom where a class template inherits from its own specialization. This technique achieves static polymorphism, offering an alternative to runtime polymorphism using virtual functions. CRTP allows customization of base class behavior without virtual function call overhead, enabling compile-time polymorphism for improved performance. It's useful when you need to extend or modify functionality in derived classes while maintaining efficiency by avoiding the runtime cost associated with virtual functions.
@@ -1,146 +1,3 @@
# Data Types in C++
# Data Types
In C++, data types are used to categorize different types of data that a program can process. They are essential for determining the type of value a variable can hold and how much memory space it will occupy. Some basic data types in C++ include integers, floating-point numbers, characters, and booleans.
## Fundamental Data Types
### Integer (int)
Integers are whole numbers that can store both positive and negative values. The size of `int` depends on the system architecture (usually 4 bytes).
Example:
int num = 42;
There are variants of `int` that can hold different ranges of numbers:
* short (`short int`): Smaller range than `int`.
* long (`long int`): Larger range than `int`.
* long long (`long long int`): Even larger range than `long int`.
### Floating-Point (float, double)
Floating-point types represent real numbers, i.e., numbers with a decimal point. There are two main floating-point types:
* **float**: Provides single-precision floating-point numbers. It typically occupies 4 bytes of memory.
Example:
float pi = 3.14f;
* **double**: Provides double-precision floating-point numbers. It consumes more memory (usually 8 bytes) but has a higher precision than `float`.
Example:
double pi_high_precision = 3.1415926535;
### Character (char)
Characters represent a single character, such as a letter, digit, or symbol. They are stored using the ASCII value of the symbol and typically occupy 1 byte of memory.
Example:
char letter = 'A';
### Boolean (bool)
Booleans represent logical values: `true` or `false`. They usually occupy 1 byte of memory.
Example:
bool is_cpp_great = true;
## Derived Data Types
Derived data types are types that are derived from fundamental data types. Some examples include:
### Arrays
Arrays are used to store multiple values of the same data type in consecutive memory locations.
Example:
int numbers[5] = {1, 2, 3, 4, 5};
int scores[10] = {100,95,98}; // Partially initialized array. 100,95,98 initialized on first 3 indexes, rest indexes are initialized with 0
int allZero[0] = {0}; // initialized all to zero
### Pointers
Pointers are used to store the memory address of a variable.
Example:
int num = 42;
int* pNum = &num;
### References
References are an alternative way to share memory locations between variables, allowing you to create an alias for another variable.
Example:
int num = 42;
int& numRef = num;
## User-Defined Data Types
User-defined data types are types that are defined by the programmer, such as structures, classes, and unions.
### Structures (struct)
Structures are used to store different data types under a single variable and accessibility of member variables and methods are public.
Example:
struct Person {
std::string name;
int age;
float height;
};
Person p1 = {"John Doe", 30, 5.9};
### Classes (class)
Classes are similar to structures, but the accessibility of the member data and function are governed by access specifiers. By default access to members of a class is private.
Example:
class Person {
public:
std::string name;
int age;
void printInfo() {
std::cout << "Name: " << name << ", Age: " << age << '\n';
};
};
Person p1;
p1.name = "John Doe";
p1.age = 30;
### Unions (union)
Unions are used to store different data types in the same memory location.
Example:
union Data {
int num;
char letter;
float decimal;
};
Data myData;
myData.num = 42;
Data types in C++ categorize the kind of values a variable can hold, influencing memory usage and allowable operations. C++ offers fundamental types like `int` for integers, `float` and `double` for floating-point numbers, `char` for single characters, and `bool` for logical values. Beyond these basics, derived types such as arrays, pointers, and references provide more complex data handling capabilities. Finally, users can define their own types using structures, classes, and unions to create custom data organizations.
@@ -1,90 +1,3 @@
# C++ Date Time
# Date / Time
In C++, you can work with dates and times using the `chrono` library, which is part of the Standard Library (STL). The `chrono` library provides various data types and functions to represent and manipulate time durations, time points, and clocks.
Duration
--------
A `duration` represents a span of time, which can be expressed in various units such as seconds, minutes, hours, etc. To create a duration, use the `std::chrono::duration` template class. Common predefined duration types are:
* `std::chrono::seconds`
* `std::chrono::minutes`
* `std::chrono::hours`
**Example:**
#include <iostream>
#include <chrono>
int main() {
std::chrono::seconds sec(5);
std::chrono::minutes min(2);
std::chrono::hours hr(1);
return 0;
}
Time Point
----------
A `time_point` represents a specific point in time. It is usually created using a combination of duration and a clock. In C++, there are three clock types provided by the `chrono` library:
* `std::chrono::system_clock`: Represents the system-wide real time wall clock.
* `std::chrono::steady_clock`: Represents a monotonic clock that is guaranteed to never be adjusted.
* `std::chrono::high_resolution_clock`: Represents the clock with the shortest tick period.
**Example:**
#include <iostream>
#include <chrono>
int main() {
std::chrono::system_clock::time_point tp = std::chrono::system_clock::now();
return 0;
}
Clock
-----
A clock provides access to the current time. It consists of the following elements:
* `time_point`: A specific point in time.
* `duration`: The time duration between two time points.
* `now()`: A static function that returns the current time point.
**Example:**
#include <iostream>
#include <chrono>
int main() {
// Get the current time_point using system_clock
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
// Get the time_point 1 hour from now
std::chrono::system_clock::time_point one_hour_from_now = now + std::chrono::hours(1);
return 0;
}
Converting Time Points to Calendar Time
---------------------------------------
To convert a time point to calendar representation, you can use the `std::chrono::system_clock::to_time_t` function.
**Example:**
#include <iostream>
#include <chrono>
#include <ctime>
int main() {
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
std::cout << "Current time: " << std::ctime(&now_c) << '\n';
return 0;
}
This summarizes the basic functionality of working with date and time in C++ using the `chrono` library. You can find more advanced features, such as casting durations and time arithmetic, in the [C++ reference](https://en.cppreference.com/w/cpp/chrono).
The C++ `chrono` library, part of the Standard Template Library (STL), offers tools for handling dates and times. It allows you to represent time spans as `durations` (e.g., seconds, minutes, hours), specific moments as `time_points` based on clocks, and access the current time using different `clock` types like `system_clock`, `steady_clock`, and `high_resolution_clock`. You can also convert `time_points` to calendar time for human-readable formats.
@@ -1,44 +1,3 @@
# Debuggers
Debuggers are essential tools for any C++ programmer, as they help in detecting, diagnosing, and fixing bugs in the code. They serve as an invaluable resource in identifying and understanding potential errors in the program.
Types of Debuggers
------------------
There are several debuggers available for use with C++:
* **GDB (GNU Debugger):** This is the most widely used C++ debugger in the Linux environment. It can debug many languages, including C and C++.
Example usage:
g++ -g main.cpp -o main # compile the code with debug info
gdb ./main # start gdb session
b main # set a breakpoint at the start of the main function
run # run the program
next # step to the next line
* **LLDB:** This is the debugger developed by LLVM. It supports multiple languages and is popular among macOS and iOS developers.
Example usage:
clang++ -g main.cpp -o main # compile the code with debug info
lldb ./main # start lldb session
breakpoint set --name main # set a breakpoint at the start of the main function
run # run the program
next # step to the next line
* **Microsoft Visual Studio Debugger:** This debugger is built into Visual Studio and is typically used in a graphical interface on Windows systems.
Example usage:
Open your Visual Studio project and go to Debug > Start Debugging. Then use the step over (F10), step into (F11), or continue (F5) commands to navigate through the code.
* **Intel Debugger (IDB):** This debugger is part of Intel's parallel development suite and is popular for high-performance applications.
* **TotalView Debugger:** Developed by Rogue Wave Software, TotalView Debugger is a commercial debugger designed for parallel, high-performance, and enterprise applications.
Each debugger has its advantages and unique features, so it's essential to choose the one that best suits your needs and works well with your development environment.
Debuggers are indispensable tools in C++ development, enabling developers to identify, analyze, and resolve defects within their code. These tools allow you to step through your code, inspect variables, and understand the program's execution flow, ultimately leading to more robust and reliable software. Several options are available, including GDB (GNU Debugger), LLDB, Microsoft Visual Studio Debugger, Intel Debugger (IDB), and TotalView Debugger, each offering unique features and catering to different development environments and application types.
@@ -1,42 +1,3 @@
# Debugger Symbols
# Debugging Symbols
Debugger symbols are additional information embedded within the compiled program's binary code, that help debuggers in understanding the structure, source code, and variable representations at a particular point in the execution process.
There are generally two types of debugging symbols:
* **Internal Debugging Symbols**: These symbols reside within the compiled binary code itself. When using internal debugging symbols, it is essential to note that the size of the binary increases, which may not be desirable for production environments.
* **External Debugging Symbols**: The debugging symbols are kept in separate files apart from the binary code, usually with file extensions such as `.pdb` (Program Database) in Windows or `.dSYM` (DWARF Symbol Information) in macOS.
Generating Debugger Symbols
---------------------------
To generate debugger symbols in C++, you need to specify specific options during the compilation process. We will use `g++` compiler as an example.
**Internal Debugging Symbols (g++)**
To create a debug build with internal debugging symbols, use the `-g` flag:
g++ -g -o my_program my_program.cpp
This command compiles `my_program.cpp` into an executable named `my_program` with internal debugging symbols.
**External Debugging Symbols (g++)**
In case you want to generate a separate file containing debugging symbols, you can use the `-gsplit-dwarf` flag:
g++ -g -gsplit-dwarf -o my_program my_program.cpp
This command compiles `my_program.cpp` into an executable named `my_program` and generates a separate file named `my_program.dwo` containing the debugging symbols.
When sharing your compiled binary to end-users, you can remove the debugging symbols using the `strip` command:
strip --strip-debug my_program
This command removes internal debug symbols, resulting in a smaller binary size while keeping the `.dwo` file for debugging purposes when needed.
Remember that the availability and syntax of these options may vary between different compilers and platforms. Be sure to consult your compiler's documentation to ensure proper usage of the debugging options.
Debugging symbols are essential for effective debugging in C++. They provide debuggers with information about the program's structure, source code relationships, and variable representations, allowing developers to step through code, inspect variables, and understand the program's behavior. These symbols can be embedded directly within the compiled binary (internal) or stored in separate files (external), such as `.pdb` on Windows or `.dSYM` on macOS. Tools like `g++` offer flags like `-g` (for internal symbols) and `-gsplit-dwarf` (for external symbols) to generate these symbols during compilation, while the `strip` command can be used to remove internal symbols from production binaries.
@@ -1,50 +1,3 @@
# Diamond Inheritance
Diamond inheritance is a specific scenario in multiple inheritance where a class is derived from two or more classes, which in turn, are derived from a common base class. It creates an ambiguity that arises from duplicating the common base class, which leads to an ambiguous behavior while calling the duplicate members.
To resolve this ambiguity, you can use virtual inheritance. A virtual base class is a class that is shared by multiple classes using `virtual` keyword in C++. This ensures that only one copy of the base class is inherited in the final derived class, and thus, resolves the diamond inheritance problem.
_Example:_
#include <iostream>
class Base {
public:
void print() {
std::cout << "Base class\n";
}
};
class Derived1 : virtual public Base {
public:
void derived1Print() {
std::cout << "Derived1 class\n";
}
};
class Derived2 : virtual public Base {
public:
void derived2Print() {
std::cout << "Derived2 class\n";
}
};
class Derived3 : public Derived1, public Derived2 {
public:
void derived3Print() {
std::cout << "Derived3 class\n";
}
};
int main() {
Derived3 d3;
d3.print(); // Now, there is no ambiguity in calling the base class function
d3.derived1Print();
d3.derived2Print();
d3.derived3Print();
return 0;
}
In the code above, `Derived1` and `Derived2` are derived from the `Base` class using virtual inheritance. So, when we create an object of `Derived3` and call the `print()` function from the `Base` class, there is no ambiguity, and the code executes without any issues.
Diamond inheritance arises in C++ when a class inherits from multiple classes that, in turn, inherit from a common base class, creating a diamond-shaped hierarchy. This can lead to ambiguity because the derived class inherits multiple copies of the base class's members. To resolve this issue, virtual inheritance is used, ensuring only one instance of the base class exists in the final derived class, eliminating ambiguity and ensuring proper member access.
@@ -1,65 +1,3 @@
# Dynamic Polymorphism
Dynamic polymorphism is a programming concept in object-oriented languages like C++ where a derived class can override or redefine methods of its base class. This means that a single method call can have different implementations based on the type of object it is called on.
Dynamic polymorphism is achieved through **virtual functions**, which are member functions of a base class marked with the `virtual` keyword. When you specify a virtual function in a base class, it can be overridden in any derived class to provide a different implementation.
Example
-------
Here's an example in C++ demonstrating dynamic polymorphism.
#include <iostream>
// Base class
class Shape {
public:
virtual void draw() {
std::cout << "Drawing a shape\n";
}
};
// Derived class 1
class Circle : public Shape {
public:
void draw() override {
std::cout << "Drawing a circle\n";
}
};
// Derived class 2
class Rectangle : public Shape {
public:
void draw() override {
std::cout << "Drawing a rectangle\n";
}
};
int main() {
Shape* shape;
Circle circle;
Rectangle rectangle;
// Storing the address of circle
shape = &circle;
// Call circle draw function
shape->draw();
// Storing the address of rectangle
shape = &rectangle;
// Call rectangle draw function
shape->draw();
return 0;
}
This code defines a base class `Shape` with a virtual function `draw`. Two derived classes `Circle` and `Rectangle` both override the `draw` function to provide their own implementations. Then in the `main` function, a pointer of type `Shape` is used to call the respective `draw` functions of `Circle` and `Rectangle` objects. The output of this program will be:
Drawing a circle
Drawing a rectangle
As you can see, using dynamic polymorphism, we can determine at runtime which `draw` method should be called based on the type of object being used.
Dynamic polymorphism is a feature in C++ that allows objects of different classes to be treated as objects of a common type. This is achieved through virtual functions, where a derived class can override a function defined in its base class. When a virtual function is called through a base class pointer or reference, the correct function implementation for the derived class object is executed at runtime, enabling flexible and extensible code.
@@ -1,62 +1,3 @@
# Dynamic Typing in C++
# Dynamic Typing
C++ is known as a statically-typed language, which means the data types of its variables are determined at compile time. However, C++ also provides concepts to have certain level of _dynamic typing_, which means determining the data types of variables at runtime.
Here is a brief overview of two ways to achieve dynamic typing in C++:
`void*` Pointers
----------------
A `void*` pointer is a generic pointer that can point to objects of any data type. They can be used to store a reference to any type of object without knowing the specific type of the object.
Example:
#include <iostream>
int main() {
int x = 42;
float y = 3.14f;
std::string z = "Hello, world!";
void* void_ptr;
void_ptr = &x;
std::cout << "int value: " << *(static_cast<int*>(void_ptr)) << '\n';
void_ptr = &y;
std::cout << "float value: " << *(static_cast<float*>(void_ptr)) << '\n';
void_ptr = &z;
std::cout << "string value: " << *(static_cast<std::string*>(void_ptr)) << '\n';
return 0;
}
`std::any` (C++17)
------------------
C++17 introduced the `std::any` class which represents a generalized type-safe container for single values of any type.
Example:
#include <iostream>
#include <any>
int main() {
std::any any_value;
any_value = 42;
std::cout << "int value: " << std::any_cast<int>(any_value) << '\n';
any_value = 3.14;
std::cout << "double value: " << std::any_cast<double>(any_value) << '\n';
any_value = std::string("Hello, world!");
std::cout << "string value: " << std::any_cast<std::string>(any_value) << '\n';
return 0;
}
Keep in mind that both `void*` pointers and `std::any` have performance implications due to the additional type checking and casting that take place during runtime. They should be used carefully and only when absolutely necessary.
While C++ is fundamentally a statically-typed language where data types are checked at compile time, it provides mechanisms to achieve a degree of dynamic typing. This involves determining the data types of variables during runtime, primarily through the use of `void*` pointers, which can point to any data type (requiring explicit casting), and the `std::any` class (introduced in C++17), a type-safe container capable of holding values of any type. Both approaches enable flexibility but require careful consideration due to potential runtime overhead and type-related errors.
@@ -1,39 +1,3 @@
# Dynamic Cast
`dynamic_cast` is a type of casting operator in C++ that is used specifically for polymorphism. It safely converts pointers and references of a base class to its derived class and checks the validity of the conversion during runtime. If the conversion is not valid (i.e., the object is not of the target type), it returns a null pointer instead of producing undefined behavior. Therefore, `dynamic_cast` can prevent potential crashes and errors when using polymorphism.
Here is a basic example of how `dynamic_cast` can be used:
#include <iostream>
class BaseClass {
public:
virtual void display() {
std::cout << "BaseClass\n";
}
};
class DerivedClass : public BaseClass {
public:
void display() {
std::cout << "DerivedClass\n";
}
};
int main() {
BaseClass *basePtr = new DerivedClass(); // Upcasting
DerivedClass *derivedPtr;
derivedPtr = dynamic_cast<DerivedClass *>(basePtr); // Downcasting
if (derivedPtr) {
derivedPtr->display(); // Output: DerivedClass
} else {
std::cout << "Invalid type conversion.";
}
delete basePtr;
return 0;
}
In this example, a pointer to a `DerivedClass` object is assigned to a `BaseClass` pointer (`basePtr`). Then, we attempt to downcast it back to a `DerivedClass` pointer using `dynamic_cast`. If the casting is successful, we can access the `DerivedClass` functionality through the new pointer (`derivedPtr`).
`dynamic_cast` is a C++ casting operator primarily used within polymorphic class hierarchies to safely convert pointers or references from a base class to a derived class. Unlike static casts, it performs a runtime check to ensure the validity of the conversion. If the object being cast is not actually an instance of the target derived class, `dynamic_cast` returns a null pointer (for pointer casts) or throws a `std::bad_cast` exception (for reference casts), preventing undefined behavior and enabling safer downcasting.
@@ -1,35 +1,3 @@
# Erase-remove idiom
# Erase-Remove
The erase-remove idiom is a common C++ technique to efficiently remove elements from a container, particularly from standard sequence containers like `std::vector`, `std::list`, and `std::deque`. It leverages the standard library algorithms `std::remove` (or `std::remove_if`) and the member function `erase()`.
The idiom consists of two steps:
* `std::remove` (or `std::remove_if`) moves the elements to be removed towards the end of the container and returns an iterator pointing to the first element to remove.
* `container.erase()` removes the elements from the container using the iterator obtained in the previous step.
Here's an example:
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> numbers = {1, 3, 2, 4, 3, 5, 3};
// Remove all occurrences of 3 from the vector.
numbers.erase(std::remove(numbers.begin(), numbers.end(), 3), numbers.end());
for (int number : numbers) {
std::cout << number << " ";
}
return 0;
}
Output:
1 2 4 5
In this example, we used the `std::remove` algorithm to remove all occurrences of the number 3 from the `std::vector<int> numbers`. After the removal, the vector contains only 1, 2, 4, and 5, as the output shows.
The Erase-Remove idiom is a common C++ technique used to efficiently remove elements from a container (like `std::vector`, `std::list`, etc.). It involves using `std::remove` (or `std::remove_if`) to move the elements to be removed to the end of the container, followed by using the container's `erase()` method to actually remove those elements, effectively shrinking the container.
@@ -1,83 +1,3 @@
# Overview
# Exception Handling
Exception handling in C++ is a mechanism to handle errors, anomalies, or unexpected events that can occur during the runtime execution of a program. This allows the program to continue running or exit gracefully when encountering errors instead of crashing abruptly.
C++ provides a set of keywords and constructs for implementing exception handling:
* `try`: Defines a block of code that should be monitored for exceptions.
* `catch`: Specifies the type of exception to be caught and the block of code that shall be executed when that exception occurs.
* `throw`: Throws an exception that will be caught and handled by the appropriate catch block.
* `noexcept`: Specifies a function that doesn't throw exceptions or terminates the program if an exception is thrown within its scope.
Example
-------
Here's an example demonstrating the basic usage of exception handling:
#include <iostream>
int divide(int a, int b) {
if (b == 0) {
throw "Division by zero!";
}
return a / b;
}
int main() {
int num1, num2;
std::cout << "Enter two numbers for division: ";
std::cin >> num1 >> num2;
try {
int result = divide(num1, num2);
std::cout << "The result is: " << result << '\n';
} catch (const char* msg) {
std::cerr << "Error: " << msg << '\n';
}
return 0;
}
In this example, we define a function `divide` that throws an exception if `b` is zero. In the `main` function, we use a `try` block to call `divide` and output the result. If an exception is thrown, it is caught inside the `catch` block, which outputs an error message. This way, we can handle the error gracefully rather than letting the program crash when attempting to divide by zero.
Standard Exceptions
-------------------
C++ provides a standard set of exception classes under the `<stdexcept>` library which can be used as the exception type for more specific error handling. Some of these classes include:
* `std::exception`: Base class for all standard exceptions.
* `std::logic_error`: Represents errors which can be detected statically by the program.
* `std::runtime_error`: Represents errors occurring during the execution of a program.
Here's an example showing how to use standard exceptions:
#include <iostream>
#include <stdexcept>
int divide(int a, int b) {
if (b == 0) {
throw std::runtime_error("Division by zero!");
}
return a / b;
}
int main() {
int num1, num2;
std::cout << "Enter two numbers for division: ";
std::cin >> num1 >> num2;
try {
int result = divide(num1, num2);
std::cout << "The result is: " << result << '\n';
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << '\n';
}
return 0;
}
In this example, we modified the `divide` function to throw a `std::runtime_error` instead of a simple string. The catch block now catches exceptions derived from `std::exception` and uses the member function `what()` to display the error message.
Exception handling in C++ is a mechanism for managing runtime errors and unexpected events, preventing abrupt program termination. It uses `try`, `catch`, and `throw` keywords to monitor code blocks for exceptions, handle specific exception types, and signal error conditions, respectively. The `noexcept` specifier ensures a function doesn't throw exceptions. Standard exception classes in `<stdexcept>`, like `std::exception`, `std::logic_error`, and `std::runtime_error`, offer a structured approach to representing and handling various error types.
@@ -1,56 +1,3 @@
# Exception Handling in C++
# Exceptions
Exception handling is a method used to tackle runtime errors so that normal flow of the program can be maintained. In C++, this is accomplished using three keywords: `try`, `catch`, and `throw`.
try { ... }
-----------
In the `try` block, you write the code that can possibly generate an exception. If an exception is encountered, the control is passed to the relevant `catch` block to handle the issue.
Example:
try {
// code that might throw an exception
}
catch (...) { ... }
-------------------
The `catch` block follows the `try` block and is responsible for handling the exceptions thrown by the `try` block. There can be multiple `catch` blocks to handle different types of exceptions.
Example:
catch (int e) {
// handle exception of type int
}
catch (char e) {
// handle exception of type char
}
catch (...) {
// handle any other exception
}
throw ... ;
-----------
In case an error occurs within the `try` block, you can use the `throw` keyword to generate an exception of the specific type. This will then be caught and handled by the corresponding `catch` block.
Example:
try {
int num1 = 10, num2 = 0;
if (num2 == 0) {
throw "Division by zero not allowed!";
} else {
int result = num1 / num2;
std::cout << "Result: " << result << '\n';
}
}
catch (const char* e) {
std::cout << "Error: " << e << '\n';
}
In summary, exception handling in C++ is a technique to handle runtime errors while maintaining the normal flow of the program. The `try`, `catch`, and `throw` keywords are used together to create the structure to deal with exceptions as they occur.
Exception handling in C++ provides a mechanism to manage runtime errors, ensuring program stability. This is achieved using `try`, `catch`, and `throw` blocks. The `try` block encloses code that might generate an exception. If an error occurs, a `throw` statement creates an exception object, which is then caught and handled by an appropriate `catch` block designed to deal with that specific exception type, thus preventing program termination.
@@ -1,59 +1,3 @@
# Exit Codes
Exit codes, also known as "return codes" or "status codes", are numeric values that a program returns to the calling environment (usually the operating system) when it finishes execution. These codes are used to indicate the success or failure of a program's execution.
0 is the standard exit code for a successful execution, while non-zero exit codes typically indicate errors or other exceptional situations. The actual meanings of non-zero exit codes can vary between different applications or systems.
In C++, you can return an exit code from the `main` function by using the `return` statement, or you can use the `exit()` function, which is part of the C++ Standard Library.
Example: Using return in `main`
-------------------------------
#include <iostream>
int main() {
// Some code here...
if (/*some error condition*/) {
std::cout << "An error occurred.\n";
return 1;
}
// More code here...
if (/*another error condition*/) {
std::cout << "Another error occurred.\n";
return 2;
}
return 0; // Successful execution
}
Example: Using the `exit()` function
------------------------------------
#include <iostream>
#include <cstdlib>
void some_function() {
// Some code here...
if (/*some error condition*/) {
std::cout << "An error occurred.\n";
std::exit(1);
}
// More code here...
}
int main() {
some_function();
// Some other code here...
return 0; // Successful execution
}
In both examples above, the program returns exit codes depending on different error conditions encountered during execution. The codes 1 and 2 are used to distinguish between the two error conditions.
Exit codes are numerical values returned by a program to the operating system upon completion, signaling whether the execution was successful or encountered errors. A return value of 0 typically indicates success, while non-zero values denote failure, with the specific meaning of each non-zero code being application-dependent. In C++, you can specify the exit code using the `return` statement within the `main` function or by calling the `exit()` function from the `<cstdlib>` header.
@@ -1 +1,3 @@
# undefined
# fmt
`fmt` is a modern formatting library for C++. It provides a simple and efficient way to format text, similar to Python's `str.format()` or C#'s `string.Format()`. It offers compile-time checks for format string correctness and generally results in faster and safer string formatting compared to traditional methods like `printf`.
@@ -1,85 +1,9 @@
# Loops in C++
# Loops
Loops are an essential concept in programming that allow you to execute a block of code repeatedly until a specific condition is met. In C++, there are three main types of loops: `for`, `while`, and `do-while`.
For Loop
--------
A `for` loop is used when you know the number of times you want to traverse through a block of code. It consists of an initialization statement, a condition, and an increment/decrement operation.
Here's the syntax for a `for` loop:
for (initialization; condition; increment/decrement) {
// block of code to execute
}
For example:
#include <iostream>
int main() {
for (int i = 0; i < 5; i++) {
std::cout << "Iteration: " << i << '\n';
}
return 0;
}
While Loop
----------
A `while` loop runs as long as a specified condition is `true`. The loop checks for the condition before entering the body of the loop.
Here's the syntax for a `while` loop:
while (condition) {
// block of code to execute
}
For example:
#include <iostream>
int main() {
int i = 0;
while (i < 5) {
std::cout << "Iteration: " << i << '\n';
i++;
}
return 0;
}
Do-While Loop
-------------
A `do-while` loop is similar to a `while` loop, with the key difference being that the loop body is executed at least once, even when the condition is `false`.
Here's the syntax for a `do-while` loop:
do {
// block of code to execute
} while (condition);
For example:
#include <iostream>
int main() {
int i = 0;
do {
std::cout << "Iteration: " << i << '\n';
i++;
} while (i < 5);
return 0;
}
In summary, loops are an integral part of C++ programming that allow you to execute a block of code multiple times. The three types of loops in C++ are `for`, `while`, and `do-while`. Each type has its own specific use case and can be chosen depending on the desired behavior.
Loops are fundamental control flow structures that enable the repetition of code blocks based on a condition. C++ provides three primary loop types: `for`, `while`, and `do-while`. The `for` loop is ideal when the number of iterations is known beforehand, while the `while` loop continues execution as long as a specified condition remains true. The `do-while` loop is similar to the `while` loop, but guarantees at least one execution of the code block, as the condition is checked at the end of the loop.
Visit the following resources to learn more:
- [@article@C++ For Loop](https://www.w3schools.com/cpp/cpp_for_loop.asp)
- [@article@C++ For Loop](https://www.w3schools.com/cpp/cpp_for_loop.asp)
- [@article@C++ While Loop](http://w3schools.com/cpp/cpp_while_loop.asp)
- [@article@C++ Do While Loop](https://www.w3schools.com/cpp/cpp_do_while_loop.asp)
@@ -1,47 +1,3 @@
# Forward Declaration
Forward declaration is a way of declaring a symbol (class, function, or variable) before defining it in the code. It helps the compiler understand the type, size, and existence of the symbol. This declaration is particularly useful when we have cyclic dependencies or to reduce compilation time by avoiding unnecessary header inclusions in the source file.
Class Forward Declaration
-------------------------
To use a class type before it is defined, you can declare the class without defining its members, like this:
class ClassA; // forward declaration
You can then use pointers or references to the class in your code before defining the class itself:
void do_something (ClassA& obj);
class ClassB {
public:
void another_function(ClassA& obj);
};
However, if you try to make an object of `ClassA` or call its member functions without defining the class, you will get a compilation error.
Function Forward Declaration
----------------------------
Functions must be declared before using them, and a forward declaration can be used to declare a function without defining it:
int add(int a, int b); // forward declaration
int main() {
int result = add(2, 3);
return 0;
}
int add(int a, int b) {
return a + b;
}
Enum and Typedef Forward Declaration
------------------------------------
For `enum` and `typedef`, it is not possible to forward declare because they don't have separate declaration and definition stages.
Keep in mind that forward declarations should be used cautiously, as they can make the code more difficult to understand.
Forward declaration is declaring an identifier (like a class, function, or variable) to the compiler before its full definition is provided. This informs the compiler about the existence and type of the identifier, allowing it to be used in limited contexts, such as pointer or reference usage, without requiring the full definition to be immediately available. This technique helps to manage dependencies, reduce compilation times, and resolve circular dependencies between different parts of a codebase.
@@ -1,48 +1,3 @@
# Full Template Specialization
Full template specialization allows you to provide a specific implementation, or behavior, for a template when used with a certain set of type parameters. It is useful when you want to handle special cases or optimize your code for specific types.
Syntax
------
To create a full specialization of a template, you need to define the specific type for which the specialization should happen. The syntax looks as follows:
template <> //Indicates that this is a specialization
className<specificType> //The specialized class for the specific type
Example
-------
Consider the following example to demonstrate full template specialization:
// Generic template
template <typename T>
class MyContainer {
public:
void print() {
std::cout << "Generic container.\n";
}
};
// Full template specialization for int
template <>
class MyContainer<int> {
public:
void print() {
std::cout << "Container for integers.\n";
}
};
int main() {
MyContainer<double> d;
MyContainer<int> i;
d.print(); // Output: Generic container.
i.print(); // Output: Container for integers.
return 0;
}
In this example, we defined a generic `MyContainer` template class along with a full specialization for `int` type. When we use the container with the `int` type, the specialized implementation's `print` method is called. For other types, the generic template implementation will be used.
Full template specialization in C++ provides a way to define specific implementations of a template (class or function) for particular types. This allows you to customize the behavior of a template when it's instantiated with a specific set of template arguments, enabling optimized code or special handling for certain types while the generic template handles other cases. This involves creating a specialized version of the template for a specific type or set of types.
@@ -1,76 +1,6 @@
# Functions in C++
# Functions
A **function** is a group of statements that perform a specific task, organized as a separate unit in a program. Functions help in breaking the code into smaller, manageable, and reusable blocks.
There are mainly two types of functions in C++:
* **Standard library functions**: Pre-defined functions available in the C++ standard library, such as `sort()`, `strlen()`, `sqrt()`, and many more. These functions are part of the standard library, so you need to include the appropriate header file to use them.
* **User-defined functions**: Functions created by the programmer to perform a specific task. To create a user-defined function, you need to define the function and call it in your code.
Defining a Function
-------------------
The general format for defining a function in C++ is:
return_type function_name(parameter list) {
// function body
}
* `return_type`: Data type of the output produced by the function. It can be `void`, indicating that the function doesn't return any value.
* `function_name`: Name given to the function, following C++ naming conventions.
* `parameter list`: List of input parameters/arguments that are needed to perform the task. It is optional, you can leave it blank when no parameters are needed.
Example
-------
#include <iostream>
// Function to add two numbers
int addNumbers(int a, int b) {
int sum = a + b;
return sum;
}
int main() {
int num1 = 5, num2 = 10;
int result = addNumbers(num1, num2); // Calling the function
std::cout << "The sum is: " << result << '\n';
return 0;
}
In this example, the function `addNumbers` takes two integer parameters, `a` and `b`, and returns the sum of the numbers. We then call this function from the `main()` function and display the result.
Function Prototypes
-------------------
In some cases, you might want to use a function before actually defining it. To do this, you need to declare a **function prototype** at the beginning of your code.
A function prototype is a declaration of the function without its body, and it informs the compiler about the function's name, return type, and parameters.
#include <iostream>
// Function prototype
int multiplyNumbers(int x, int y);
int main() {
int num1 = 3, num2 = 7;
int result = multiplyNumbers(num1, num2); // Calling the function
std::cout << "The product is: " << result << '\n';
return 0;
}
// Function definition
int multiplyNumbers(int x, int y) {
int product = x * y;
return product;
}
In this example, we use a function prototype for `multiplyNumbers()` before defining it. This way, we can call the function from the `main()` function even though it hasn't been defined yet in the code.
Functions in C++ are self-contained blocks of code designed to perform specific tasks, promoting code reusability and modularity. They come in two main types: standard library functions (pre-built functions like `sort()` and `sqrt()`) and user-defined functions (created by the programmer). Defining a function involves specifying its return type, name, and a list of parameters it accepts. To use a function before its definition, a function prototype can be declared, informing the compiler about the function's signature.
Visit the following resources to learn more:
@@ -1,77 +1,3 @@
# GDB: GNU Debugger
GDB, or the GNU Project Debugger, is a powerful command-line debugger used primarily for C, C++, and other languages. It can help you find runtime errors, examine the program's execution state, and manipulate the flow to detect and fix bugs easily.
Getting started with GDB
------------------------
To start using GDB, you first need to compile your code with the `-g` flag, which includes debugging information in the executable:
g++ -g myfile.cpp -o myfile
Now, you can load your compiled program into GDB:
gdb myfile
Basic GDB Commands
------------------
Here are some common GDB commands you'll find useful when debugging:
* `run`: Start your program.
* `break [function/line number]`: Set a breakpoint at the specified function or line.
* `continue`: Continue the program execution after stopping on a breakpoint.
* `next`: Execute the next line of code, stepping over function calls.
* `step`: Execute the next line of code, entering function calls.
* `print [expression]`: Evaluate an expression in the current context and display its value.
* `backtrace`: Show the current call stack.
* `frame [frame-number]`: Switch to a different stack frame.
* `quit`: Exit GDB.
Example Usage
-------------
Suppose you have a simple `cpp` file called `example.cpp`:
#include <iostream>
void my_function(int i) {
std::cout << "In my_function with i = " << i << '\n';
}
int main() {
for (int i = 0; i < 5; ++i) {
my_function(i);
}
return 0;
}
First, compile the code with debugging symbols:
g++ -g example.cpp -o example
Start GDB and load the `example` program:
gdb example
Set a breakpoint in the `my_function` function and run the program:
(gdb) break my_function
(gdb) run
Once stopped at the breakpoint, use `next`, `print`, and `continue` to examine the program's state:
(gdb) next
(gdb) print i
(gdb) continue
Finally, exit GDB with the `quit` command.
This was just a brief summary of GDB; you can find more details in the [official GDB manual](https://sourceware.org/gdb/current/onlinedocs/gdb/).
GDB, the GNU Project Debugger, is a command-line tool essential for debugging C and C++ programs. It allows developers to examine a program's execution, set breakpoints, step through code, inspect variables, and analyze call stacks to identify and fix bugs. By using GDB, developers gain deep insight into the runtime behavior of their programs, making it an indispensable part of the C++ development workflow.
@@ -1 +1,3 @@
# undefined
# gRPC
gRPC is a high-performance, open-source framework developed by Google for building remote procedure calls (RPCs). It uses Protocol Buffers as its Interface Definition Language (IDL) and relies on HTTP/2 for transport. gRPC enables client applications to directly call methods on a server application on a different machine as if it were a local object, making it easier to create distributed applications and microservices.
@@ -1,52 +1,3 @@
# Code Splitting
# Headers / CPP Files
Code splitting refers to the process of breaking down a large code base into smaller, more manageable files or modules. This helps improve the organization, maintainability, and readability of the code. In C++, code splitting is generally achieved through the use of separate compilation, header files, and source files.
### Header Files (.h or .hpp)
Header files, usually with the `.h` or `.hpp` extension, are responsible for declaring classes, functions, and variables that are needed by multiple source files. They act as an interface between different parts of the code, making it easier to manage dependencies and reduce the chances of duplicated code.
Example of a header file:
// example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
class Example {
public:
void printMessage();
};
#endif
### Source Files (.cpp)
Source files, with the `.cpp` extension, are responsible for implementing the actual functionality defined in the corresponding header files. They include the header files as needed and provide the function and class method definitions.
Example of a source file:
// example.cpp
#include "example.h"
#include <iostream>
void Example::printMessage() {
std::cout << "Hello, code splitting!\n";
}
### Separate Compilation
C++ allows for separate compilation, which means that each source file can be compiled independently into an object file. These object files can then be linked together to form the final executable. This provides faster build times when making changes to a single source file since only that file needs to be recompiled, and the other object files can be reused.
Example of separate compilation and linking:
# Compile each source file into an object file
g++ -c main.cpp -o main.o
g++ -c example.cpp -o example.o
# Link object files together to create the executable
g++ main.o example.o -o my_program
By following the code splitting technique, you can better organize your C++ codebase, making it more manageable and maintainable.
In C++, organizing code effectively involves splitting programs into header and source files. Header files (with extensions like `.h` or `.hpp`) declare interfaces such as classes, functions, and variables, acting as blueprints for other parts of the code. Source files (with the `.cpp` extension) then implement the functionality declared in the headers. This separation supports modularity, reduces compilation times through separate compilation, and enhances code readability and maintainability by clearly defining interfaces and implementations.
@@ -1,104 +1,3 @@
# C++ Idioms
C++ idioms are well-established patterns or techniques that are commonly used in C++ programming to achieve a specific outcome. They help make code efficient, maintainable, and less error-prone. Here are some of the common C++ idioms:
1\. Resource Acquisition is Initialization (RAII)
-------------------------------------------------
This idiom ensures that resources are always properly acquired and released by tying their lifetime to the lifetime of an object. When the object gets created, it acquires the resources and when it gets destroyed, it releases them.
class Resource {
public:
Resource() { /* Acquire resource */ }
~Resource() { /* Release resource */ }
};
void function() {
Resource r; // Resource is acquired
// ...
} // Resource is released when r goes out of scope
2\. Rule of Three
-----------------
If a class defines any one of the following, it should define all three: copy constructor, copy assignment operator, and destructor.
class MyClass {
public:
MyClass();
MyClass(const MyClass& other); // Copy constructor
MyClass& operator=(const MyClass& other); // Copy assignment operator
~MyClass(); // Destructor
};
3\. Rule of Five
----------------
With C++11, the rule of three was extended to five, covering move constructor and move assignment operator.
class MyClass {
public:
MyClass();
MyClass(const MyClass& other); // Copy constructor
MyClass(MyClass&& other); // Move constructor
MyClass& operator=(const MyClass& other); // Copy assignment operator
MyClass& operator=(MyClass&& other); // Move assignment operator
~MyClass(); // Destructor
};
4\. PImpl (Pointer to Implementation) Idiom
-------------------------------------------
This idiom is used to separate the implementation details of a class from its interface, resulting in faster compile times and the ability to change implementation without affecting clients.
// header file
class MyClass {
public:
MyClass();
~MyClass();
void someMethod();
private:
class Impl;
Impl* pImpl;
};
// implementation file
class MyClass::Impl {
public:
void someMethod() { /* Implementation */ }
};
MyClass::MyClass() : pImpl(new Impl()) {}
MyClass::~MyClass() { delete pImpl; }
void MyClass::someMethod() { pImpl->someMethod(); }
5\. Non-Virtual Interface (NVI)
-------------------------------
This enforces a fixed public interface and allows subclasses to only override specific private or protected virtual methods.
class Base {
public:
void publicMethod() {
// Common behavior
privateMethod(); // Calls overridden implementation
}
protected:
virtual void privateMethod() = 0; // Pure virtual method
};
class Derived : public Base {
protected:
virtual void privateMethod() override {
// Derived implementation
}
};
These are just a few examples of the many idioms in C++ programming. They can provide guidance when designing and implementing your code, but it's essential to understand the underlying concepts to adapt them to different situations.
C++ idioms are established patterns and techniques used by developers to solve common programming challenges in a standardized and effective manner. These idioms promote code that is more efficient, maintainable, and less prone to errors by leveraging the features and principles of C++. Examples include Resource Acquisition Is Initialization (RAII) for managing resources, the Rule of Five for proper object lifecycle management, the PImpl idiom for hiding implementation details, and the Non-Virtual Interface (NVI) pattern for controlling inheritance behavior. Understanding and applying these idioms is crucial for writing robust and high-quality C++ code.
@@ -1,85 +1,9 @@
# If-Else / Switch / Goto
# Conditional Statements and Unconditional Jump
C++ provides you with tools which helps you to control the way your program behaves (logic flows) based on how the user interact with your program. Here we will discuss about `if-else`, `switch` and `goto` are three common ways to guide the flow of logic in your code.
### if-else
Use if-else when you want your program to choose between two possible actions.
#include <iostream>
int main() {
int age = 18;
if (age >= 18) {
std::cout << "You can vote!" << std::endl;
} else {
std::cout << "Too young to vote." << std::endl;
}
return 0;
}
Explanation: The program checks the condition inside if. If it’s true, the first block runs. If it’s false, the else block runs.
### switch
Use switch when you have multiple possible outcomes for one variable. It’s often cleaner than using many if-else statements.
#include <iostream>
int main() {
int day = 3;
switch (day) {
case 1: std::cout << "Monday"; break;
case 2: std::cout << "Tuesday"; break;
case 3: std::cout << "Wednesday"; break;
default: std::cout << "Invalid day";
}
return 0;
}
Explanation:
* The value of "day" is compared with each "case".
* When a match is found, that block runs.
* "break" stops further checking.
* "default" runs if no case matches.
### goto
The goto statement allows you to jump directly to another part of your code. Although it works, it is not recommended because it makes programs harder to understand and maintain.
#include <iostream>
int main() {
int x = 5;
if (x == 5)
goto label;
std::cout << "This line will be skipped." << std::endl;
label:
std::cout << "Jumped to label!" << std::endl;
return 0;
}
Explaination:
* The keyword goto tells the program to jump to a specific place marked by a label (for example, label:).
* When the program reaches goto label;, it skips everything in between and continues execution from the label.
* * *
`if-else`, `switch`, and `goto` are fundamental control flow mechanisms in C++. `if-else` constructs enable programs to execute different code blocks based on boolean conditions. The `switch` statement provides a way to efficiently handle multiple possible values of a single variable. The `goto` statement allows for an unconditional jump to a labeled point in the code, but its use is generally discouraged due to potential impacts on code readability and maintainability.
Visit the following resources to learn more:
- [@article@The 'if-else' Statement in C++](https://www.youtube.com/watch?v=9-BjXs1vMSc)
- [@article@Learn C++ With Me - Switch Statement](https://www.youtube.com/watch?v=uOlLs1OYSSI)
- [@article@Why is it illegal to use "goto"?](https://youtu.be/AKJhThyTmQw?si=gjEqAsDZVMDGVAT2)
- [@video@The 'if-else' Statement in C++](https://www.youtube.com/watch?v=9-BjXs1vMSc)
- [@video@Learn C++ With Me - Switch Statement](https://www.youtube.com/watch?v=uOlLs1OYSSI)
- [@video@Why is it illegal to use "goto"?](https://youtu.be/AKJhThyTmQw?si=gjEqAsDZVMDGVAT2)
@@ -1,52 +1,3 @@
# Installing C++
Before you can start programming in C++, you will need to have a compiler installed on your system. A compiler is a program that converts the C++ code you write into an executable file that your computer can run. There are several popular C++ compilers to choose from, depending on your operating system and preference.
### Windows
For Windows, one popular option is to install the [Microsoft Visual Studio IDE](https://visualstudio.microsoft.com/vs/), which includes the Microsoft Visual C++ compiler (MSVC).
Alternatively, you can also install the [MinGW-w64](https://mingw-w64.org/) compiler system, which is a Windows port of the GNU Compiler Collection (GCC). To install MinGW-w64, follow these steps:
* Download the installer from [here](https://sourceforge.net/projects/mingw-w64/files/).
* Run the installer and select your desired architecture, version, and install location.
* Add the `bin` folder inside the installation directory to your system's `PATH` environment variable.
### macOS
For macOS, you can install the Apple LLVM `clang` compiler which is part of the Xcode Command Line Tools. To do this, open a terminal and enter:
xcode-select --install
This will prompt a dialog to install the Command Line Tools, which includes the `clang` compiler.
### Linux
On Linux, you can install the GNU Compiler Collection (GCC) through your distribution's package manager. Here are some examples for popular Linux distributions:
* Ubuntu, Debian, and derivatives:
sudo apt-get install g++ build-essential
* Fedora, CentOS, RHEL, and derivatives:
sudo dnf install gcc-c++ make
* Arch Linux and derivatives:
sudo pacman -S gcc make
### Checking the Installation
To confirm that the compiler is installed and available on your system, open a terminal/command prompt, and enter the following command:
g++ --version
You should see output displaying the version of your installed C++ compiler.
Now you're ready to start writing and compiling your C++ code!
To use C++, you need to set up the necessary tools on your system to compile and run C++ code. This typically includes a compiler (like GCC or Clang), a build system (like Make or CMake), and an Integrated Development Environment (IDE) or text editor for writing code. The specific steps vary depending on your operating system (Windows, macOS, or Linux).
@@ -1,139 +1,9 @@
# Introduction to C++
# Introduction to Language
C++ is a general-purpose, high-performance programming language. It was developed by Bjarne Stroustrup at Bell Labs starting in 1979. C++ is an extension of the C programming language, adding features such as classes, objects, and exceptions.
Basics of C++ Programming
-------------------------
Here are some basic components and concepts in C++ programming:
Including Libraries
-------------------
In C++, we use the `#include` directive to include libraries or header files into our program. For example, to include the standard input/output library, we write:
#include <iostream>
Main Function
-------------
The entry point of a C++ program is the `main` function. Every C++ program must have a `main` function:
int main() {
// Your code goes here
return 0;
}
Input/Output
------------
To perform input and output operations in C++, we can use the built-in objects `std::cin` for input and `std::cout` for output, available in the `iostream` library. Here's an example of reading an integer and printing its value:
#include <iostream>
int main() {
int number;
std::cout << "Enter an integer: ";
std::cin >> number;
std::cout << "You entered: " << number << '\n';
return 0;
}
Variables and Data Types
------------------------
C++ has several basic data types for representing integer, floating-point, and character values:
* `int`: integer values
* `float`: single-precision floating-point values
* `double`: double-precision floating-point values
* `char`: single characters
* `bool`: boolean values
Variables must be declared with a data type before they can be used:
int x;
float y;
double z;
char c;
bool b;
Control Structures
------------------
C++ provides control structures for conditional execution and iteration, such as `if`, `else`, `while`, `for`, and `switch` statements.
### If-Else Statement
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
### While Loop
while (condition) {
// Code to execute while the condition is true
}
### For Loop
for (initialization; condition; update) {
// Code to execute while the condition is true
}
### Switch Statement
switch (variable) {
case value1:
// Code to execute if variable == value1
break;
case value2:
// Code to execute if variable == value2
break;
// More cases...
default:
// Code to execute if variable does not match any case value
}
Functions
---------
Functions are reusable blocks of code that can be called with arguments to perform a specific task. Functions are defined with a return type, a name, a parameter list, and a body.
ReturnType functionName(ParameterType1 parameter1, ParameterType2 parameter2) {
// Function body
// ...
return returnValue;
}
For example, here's a function that adds two integers and returns the result:
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(3, 4);
std::cout << "3 + 4 = " << result << '\n';
return 0;
}
This basic introduction to C++ should provide you with a good foundation for further learning. Explore more topics such as classes, objects, inheritance, polymorphism, templates, and the Standard Template Library (STL) to deepen your understanding of C++ and start writing more advanced programs.
C++ is a general-purpose programming language known for its performance, efficiency, and control over hardware. It supports both procedural and object-oriented programming paradigms, making it versatile for a wide range of applications, from system programming to game development. C++ builds upon the C language, adding features like classes, templates, and exception handling.
Visit the following resources to learn more:
- [@article@LearnC++](https://www.learncpp.com/)
- [@article@Learn C++](https://www.learncpp.com/)
- [@video@C++ Full Course by freeCodeCamp](https://youtu.be/vLnPwxZdW4Y)
- [@video@C++ C++ Programming Course - Beginner to Advanced](https://www.youtube.com/watch?v=8jLOx1hD3_o)
@@ -1,44 +1,3 @@
# iostream
`iostream` is a header in the C++ Standard Library that provides functionality for basic input and output (I/O) operations. The I/O streams facilitate communication between your program and various sources, such as the console, files, or other programs.
`iostream` includes the following classes:
* `istream`: for input operations from an input source.
* `ostream`: for output operations to an output target.
* `iostream`: a combination of `istream` and `ostream` for both input and output operations.
These classes inherit from base classes `ios` and `ios_base`.
Additionally, `iostream` defines several objects that are instances of these classes and represent the standard input and output streams:
* `cin`: an `istream` object to read from the standard input, typically corresponding to the keyboard.
* `cout`: an `ostream` object to write to the standard output, typically the console.
* `cerr`: an `ostream` object to write to the standard error output, typically used for displaying error messages.
* `clog`: an `ostream` object, similar to `cerr`, but its output can be buffered.
Here are some code examples on using `iostream` for input and output operations:
#include <iostream>
int main() {
int a;
std::cout << "Enter a number: ";
std::cin >> a;
std::cout << "You entered: " << a << '\n';
return 0;
}
#include <iostream>
int main() {
std::cerr << "An error occurred.\n";
std::clog << "Logging information.\n";
return 0;
}
Remember to include the `iostream` header when using these features:
#include <iostream>
`iostream` is a fundamental header in the C++ Standard Library that handles input and output (I/O) operations. It provides classes like `istream` for input, `ostream` for output, and `iostream` for combined I/O. `cin`, `cout`, `cerr`, and `clog` are predefined objects within `iostream` for standard input, standard output, standard error output, and buffered logging, respectively. Use the `#include <iostream>` directive to incorporate this functionality into your programs.
@@ -1,87 +1,3 @@
# Iterators
Iterators are objects in the C++ Standard Library (`STL`) that help us traverse containers like arrays, lists, and vectors. Essentially, they act as a bridge between container classes and algorithms. Iterators behave similar to pointers but provide a more generalized and abstract way of accessing elements in a container.
There are different types of iterators which you would encounter depending on their use cases:
* **Input Iterator**: Used to read elements in a container only once, in a forward direction. They cannot modify elements.
Example:
std::vector<int> nums = {1, 2, 3, 4};
std::istream_iterator<int> input(std::cin);
std::copy(input, std::istream_iterator<int>(), std::back_inserter(nums));
* **Output Iterator**: Used to write elements in a container only once, in a forward direction. They cannot re-write elements.
Example:
std::vector<int> nums = {1, 2, 3, 4};
std::ostream_iterator<int> output(std::cout, ", ");
std::copy(nums.begin(), nums.end(), output);
* **Forward Iterator**: Similar to input iterators but can be used for multiple passes over the elements in a container. They cannot move backward.
Example:
std::forward_list<int> nums = {1, 2, 3, 4};
std::forward_list<int>::iterator itr = nums.begin();
while (itr != nums.end()) {
std::cout << *itr << " ";
++itr;
}
**Reverse Iterator**: Similar to input iterators but can be used for multiple passes over the elements in a container. They cannot move forward.
Example:
std::list<int> nums = {1, 2, 3, 4};
std::list<int>::reverse_iterator itr = nums.rbegin();
while (itr != nums.rend()) {
std::cout << *itr << " ";
++itr;
}
* **Bidirectional Iterator**: These iterators offer the ability to move both forward and backward in a container. List and set containers have bi-directional iterators.
Example:
std::list<int> nums = {1, 2, 3, 4};
std::list<int>::iterator itr;
for (itr = nums.begin(); itr != nums.end(); ++itr) {
std::cout << *itr << " ";
}
for (--itr; itr != nums.begin(); --itr) {
std::cout << *itr << " ";
}
* **Random Access Iterator**: These iterators provide the most flexible ways to access elements in a container. They can move forwards, backwards, jump directly to other elements, and access elements at a given index.
Example:
std::vector<int> nums = {1, 2, 3, 4};
std::vector<int>::iterator itr;
for (itr = nums.begin(); itr != nums.end(); ++itr) {
std::cout << *itr << " ";
}
for (itr -= 1; itr != nums.begin() - 1; --itr) {
std::cout << *itr << " ";
}
For most cases, you would want to start with the `auto` keyword and the appropriate container methods (like `begin()` and `end()`) to work with iterators.
Example:
std::vector<int> nums = {1, 2, 3, 4};
for (auto itr = nums.begin(); itr != nums.end(); ++itr) {
std::cout << *itr << " ";
}
When working with algorithms, remember that the C++ Standard Library provides various algorithms that already utilize iterators for tasks like searching, sorting, and manipulating elements.
Iterators in C++'s Standard Template Library (STL) are essential tools for traversing and accessing elements within containers like vectors, lists, and arrays. They act as generalized pointers, offering a way to interact with container elements without needing to know the container's underlying implementation. C++ provides different iterator types, including input, output, forward, bidirectional, reverse, and random access iterators, each with specific capabilities for reading, writing, and navigating through container elements. For most use cases, you would use `auto` keyword with `begin()` and `end()` methods to work with iterators. C++ algorithms often leverage iterators for tasks such as searching and sorting.
@@ -1,62 +1,6 @@
# Lambda Functions in C++
# Lambdas
A lambda function, or simply "lambda", is an anonymous (unnamed) function that is defined in place, within your source code, and with a concise syntax. Lambda functions were introduced in C++11 and have since become a widely used feature, especially in combination with the Standard Library algorithms.
Syntax
------
Here is a basic syntax of a lambda function in C++:
[capture-list](parameters) -> return_type {
// function body
};
* **capture-list**: A list of variables from the surrounding scope that the lambda function can access.
* **parameters**: The list of input parameters, just like in a regular function. Optional.
* **return\_type**: The type of the value that the lambda function will return. This part is optional, and the compiler can deduce it in many cases.
* **function body**: The code that defines the operation of the lambda function.
Usage Examples
--------------
Here are a few examples to demonstrate the use of lambda functions in C++:
### Lambda function with no capture, parameters, or return type.
auto printHello = []() {
std::cout << "Hello, World!\n";
};
printHello(); // Output: Hello, World!
### Lambda function with parameters.
auto add = [](int a, int b) {
return a + b;
};
int result = add(3, 4); // result = 7
### Lambda function with capture-by-value.
int multiplier = 3;
auto times = [multiplier](int a) {
return a * multiplier;
};
int result = times(5); // result = 15
### Lambda function with capture-by-reference.
int expiresInDays = 45;
auto updateDays = [&expiresInDays](int newDays) {
expiresInDays = newDays;
};
updateDays(30); // expiresInDays = 30
Note that, when using the capture by reference, any change made to the captured variable _inside_ the lambda function will affect its value in the surrounding scope.
Lambdas, also known as lambda expressions, are a concise way to define anonymous functions directly within the code where they are needed. They are essentially nameless functions that can capture variables from the surrounding scope. Lambdas are particularly useful for short, self-contained operations, making code more readable and efficient by eliminating the need to define separate named functions for simple tasks, often used with algorithms like `std::sort` or `std::for_each`.
Visit the following resources to learn more:
@@ -1,147 +1,3 @@
# Language Concepts in C++
# Language Concepts
C++ is a powerful, high-level, object-oriented programming language that offers several key language concepts. These concepts provide the foundation upon which you can build efficient, reliable, and maintainable programs. Here's a brief summary of some important language concepts in C++.
Variables and Data Types
------------------------
C++ provides various fundamental data types such as `int`, `float`, `double`, `char`, and `bool` to declare and manipulate variables in a program.
Example:
int age = 25;
float height = 1.7f;
double salary = 50000.0;
char grade = 'A';
bool isEmployed = true;
Control Structures
------------------
Control structures enable you to control the flow of execution of a program. Key control structures in C++ include:
* Conditional statement: `if`, `else`, and `else if`
* Loop constructs: `for`, `while`, and `do-while`
* Switch-case construct
Example:
// If-else statement
if (age > 18) {
std::cout << "You are eligible to vote.";
} else {
std::cout << "You are not eligible to vote.";
}
// For loop
for (int i = 0; i < 5; i++) {
std::cout << "Hello World!";
}
Functions
---------
Functions in C++ allow you to break down a large program into small, manageable, and reusable pieces of code.
Example:
int add(int a, int b) {
return a + b;
}
int main() {
int sum = add(10, 20);
std::cout << "The sum is: " << sum;
return 0;
}
Arrays and Vectors
------------------
Arrays and Vectors are commonly used data structures to store and manipulate a collection of elements of the same datatype.
Example:
// Array
int marks[] = {90, 80, 95, 85};
// Vector
std::vector<int> scores = {10, 20, 30, 40};
Pointers
--------
Pointers are variables that store memory addresses of other variables. They enable more efficient handling of memory, and are useful for working with dynamic data structures.
Example:
int num = 10;
int* p = &num; // p stores the address of num
Structures and Classes
----------------------
Structures and Classes are user-defined data types that allow grouping of variables and functions under a single name.
Example:
// Structure
struct Student {
std::string name;
int age;
};
// Class
class Employee {
public:
std::string name;
int age;
void displayInfo() {
std::cout << "Name: " << name << "\nAge: " << age;
}
};
Inheritance and Polymorphism
----------------------------
Inheritance is a mechanism that allows a class to inherit properties and methods from a base class. Polymorphism enables you to use a base class type to represent derived class objects.
Example:
class Base {
public:
void display() {
std::cout << "This is the base class.";
}
};
class Derived : public Base {
public:
void display() {
std::cout << "This is the derived class.";
}
};
Exception Handling
------------------
C++ provides a mechanism to handle exceptions(runtime errors) gracefully using `try`, `catch`, and `throw` constructs.
Example:
try {
// Code that might throw an exception
int result = a / b;
} catch (const exception &e) {
std::cout << "Caught an exception: " << e.what();
}
These are some of the key language concepts in C++, which will help you to understand the language better and develop efficient and maintainable applications.
C++'s foundation rests on several core language concepts that enable the creation of efficient and robust applications. These concepts encompass fundamental building blocks like variables and data types for storing information, control structures for managing program flow with conditionals and loops, and functions for modularizing code into reusable blocks. Furthermore, C++ provides data structures such as arrays and vectors for managing collections of data, powerful memory manipulation through pointers, and object-oriented programming features via structures and classes, supporting inheritance and polymorphism. Finally, exception handling allows for graceful recovery from runtime errors, creating a well-rounded and reliable programming experience.
@@ -0,0 +1,3 @@
# Language Tools
Language tools in C++ provide functionalities that aid developers in writing, analyzing, and maintaining C++ code. These tools often include compilers, debuggers, static analyzers, and build systems that help to automate tasks, identify potential errors, and improve the overall quality of the codebase. They streamline the development process, from initial coding to final deployment.
@@ -1,37 +1,3 @@
# Inclusion in C++
# Library Inclusion
In C++ programming, inclusion refers to incorporating external libraries, header files, or other code files into your program. This process allows developers to access pre-built functions, classes, and variable declarations that can be used in their own code. There are two types of inclusion in C++:
* Header Inclusion
* Source Inclusion
### Header Inclusion
Header inclusion involves including header files using the preprocessor directive `#include`. Header files are typically used to provide function prototypes, class declarations, and constant definitions that can be shared across multiple source files. There are two ways to include header files in your program:
* Angle brackets `<>`: Used for including standard library headers, like `iostream`, `vector`, or `algorithm`.
Example:
#include <iostream>
#include <vector>
* Double quotes `""`: Used for including user-defined headers or headers provided by third-party libraries.
Example:
#include "myHeader.h"
#include "thirdPartyLibrary.h"
### Source Inclusion
Source inclusion refers to including the content of a source file directly in another source file. This approach is generally not recommended as it can lead to multiple definitions and increased compile times but it can occasionally be useful for certain tasks (e.g., templates or simple small programs). To include a source file, you can use the `#include` directive with double quotes, just like with header files:
Example:
#include "mySourceFile.cpp"
Remember, using source inclusion for large projects or in situations where it's not necessary can lead to unexpected issues and should be avoided.
In C++, library inclusion is the process of making external code available to your program. This is achieved primarily through the `#include` preprocessor directive, which allows you to incorporate header files containing declarations of functions, classes, and other entities. Header files from the standard library are included using angle brackets (`<iostream>`), while user-defined or third-party headers are included using double quotes (`"myheader.h"`). While less common, source files can also be included, though this practice is generally discouraged due to potential issues with multiple definitions and longer compile times.
@@ -1,41 +1,3 @@
# Licensing in C++
# Licensing
Licensing is a crucial aspect of working with libraries in C++ because it determines the rights and limitations on how you can use, modify, and distribute a given library. There are various types of licenses applied to open-source libraries. Below is a brief overview of three common licenses:
MIT License
-----------
The MIT License is a permissive license that allows users to do whatever they want with the software code. They only need to include the original copyright, license notice, and a disclaimer of warranty in their copies.
Example: Including the MIT License into your project can be done by simply adding the license file and a notice at the top of your source code files like:
/* Copyright (C) [year] [author]
* SPDX-License-Identifier: MIT
*/
GNU General Public License (GPL)
--------------------------------
The GPL is a copyleft license that grants users the rights to use, study, share, and modify the software code. However, any changes made to the code or any software that uses GPL licensed code must also be distributed under the GPL license.
Example: To include a GPL license in your project, include a `COPYING` file with the full text of the license and place a notice in your source code files like:
/* Copyright (C) [year] [author]
* SPDX-License-Identifier: GPL-3.0-or-later
*/
Apache License 2.0
------------------
The Apache License is a permissive license similar to the MIT license and allows users to do virtually anything with the software code. The primary difference is that it requires that any changes to the code are documented, and it provides specific terms for patent protection.
Example: To include the Apache License in your project, add a `LICENSE` file with the full text of the license. Add a notice to your source code files like:
/* Copyright (C) [year] [author]
* SPDX-License-Identifier: Apache-2.0
*/
Please note that these are brief summaries of the licenses, and there are many other licenses available for use in software projects. When using third-party libraries, it is crucial to understand and adhere to the terms of their respective licenses to avoid legal complications.
Licensing governs how you can use, modify, and distribute software libraries, particularly crucial when integrating third-party code into your C++ projects. Common open-source licenses like MIT, GPL, and Apache 2.0 each provide different levels of freedom and obligations. The MIT License is permissive, allowing almost any use with minimal requirements. The GPL is copyleft, requiring that modifications and derivative works also be licensed under GPL. The Apache License 2.0 is permissive, but includes terms relating to patents and requires documentation of modifications. Understanding and adhering to these licenses is essential to avoid legal issues.
@@ -1,37 +1,3 @@
# Object Lifetime in C++
# Lifetime of Objects
Object lifetime refers to the time during which an object exists, from the moment it is created until it is destroyed. In C++, an object's lifetime can be classified into four categories:
* **Static Storage Duration**: Objects with static storage duration exist for the entire run of the program. These objects are allocated at the beginning of the program's run and deallocated when the program terminates. Global variables, static data members, and static local variables fall into this category.
int global_var; // Static storage duration
class MyClass {
static int static_var; // Static storage duration
};
void myFunction() {
static int local_var; // Static storage duration
}
* **Thread Storage Duration**: Objects with thread storage duration exist for the lifetime of the thread they belong to. They are created when a thread starts and destroyed when the thread exits. Thread storage duration can be specified using the `thread_local` keyword.
thread_local int my_var; // Thread storage duration
* **Automatic Storage Duration**: Objects with automatic storage duration are created at the point of definition and destroyed when the scope in which they are declared is exited. These objects are also known as "local" or "stack" objects. Function parameters and local non-static variables fall into this category.
void myFunction() {
int local_var; // Automatic storage duration
}
* **Dynamic Storage Duration**: Objects with dynamic storage duration are created at runtime, using memory allocation functions such as `new` or `malloc`. The lifetime of these objects must be managed manually, as they are not automatically deallocated when the scope is exited. Instead, it is the programmer's responsibility to destroy the objects using the `delete` or `free` functions when they are no longer needed, to avoid memory leaks.
int* ptr = new int; // Dynamic storage duration
delete ptr;
Understanding object lifetimes is essential for managing memory efficiently in C++ programs and avoiding common issues like memory leaks and undefined behavior.
Keep in mind that a proper understanding of constructors and destructors for classes is also essential when working with objects of varying lifetimes, as they allow you to control the behavior of object creation and destruction.
Object lifetime in C++ dictates when an object comes into existence and when it ceases to exist. This crucial concept influences memory management and program correctness. C++ defines four storage durations: static (exists for the program's duration), thread (exists for a thread's duration), automatic (exists within a scope), and dynamic (controlled by `new` and `delete`). Managing object lifetimes effectively, especially dynamic objects, is essential for preventing memory leaks and ensuring stable application behavior.
@@ -1,56 +1,3 @@
# Logical Operators in C++
# Logical Operators
Logical operators are used to perform logical operations on the given expressions, mostly to test the relationship between different variables or values. They return a boolean value i.e., either true (1) or false (0) based on the result of the evaluation.
C++ provides the following logical operators:
* **AND Operator (&&)** The AND operator checks if both the operands/conditions are true, then the expression is true. If any one of the conditions is false, the whole expression will be false.
(expression1 && expression2)
Example:
int a = 5, b = 10;
if (a > 0 && b > 0) {
std::cout << "Both values are positive.\n";
}
* **OR Operator (||)** The OR operator checks if either of the operands/conditions are true, then the expression is true. If both the conditions are false, it will be false.
(expression1 || expression2)
Example:
int a = 5, b = -10;
if (a > 0 || b > 0) {
std::cout << "At least one value is positive.\n";
}
* **NOT Operator (!)** The NOT operator reverses the result of the condition/expression it is applied on. If the condition is true, the NOT operator will make it false and vice versa.
!(expression)
Example:
int a = 5;
if (!(a < 0)) {
std::cout << "The value is not negative.\n";
}
Using these operators, you can create more complex logical expressions, for example:
int a = 5, b = -10, c = 15;
if (a > 0 && (b > 0 || c > 0)) {
std::cout << "At least two values are positive.\n";
}
This covers the essential information about logical operators in C++.
Logical operators in C++ allow you to combine or modify boolean expressions, resulting in a final boolean value of either true (1) or false (0). These operators are crucial for controlling the flow of your program based on multiple conditions. C++ provides three main logical operators: AND (`&&`), which returns true only if both operands are true; OR (`||`), which returns true if at least one operand is true; and NOT (`!`), which reverses the boolean value of its operand. They enable you to create complex conditional statements.
@@ -1,53 +1,3 @@
# C++ Macros
Macros are preprocessing directives in C++ used by the preprocessor to perform text substitution. They are defined using the `#define` directive, followed by the macro name and the value to be substituted.
Macros can be used to define constants, create function-like macros, or perform conditional compilation.
Constant Macros
---------------
Constant macros are used to define symbolic constants for use in code. They do not use any memory and are replaced by the preprocessor before the compilation process.
Example:
#define PI 3.14159
This macro defines a symbolic constant `PI`. You can use it in your code as if it were a regular variable.
double circumference = 2 * PI * radius;
Function-like Macros
--------------------
Function-like macros are similar to regular functions. They take a list of arguments and perform text substitution.
Example:
#define SQUARE(x) ((x) * (x))
This macro defines a function-like macro `SQUARE` that calculates the square of a number.
int square_of_five = SQUARE(5); // expands to ((5) * (5))
Conditional Compilation
-----------------------
Macros can be used for conditional compilation using the `#ifdef`, `#ifndef`, `#if`, `#else`, `#elif`, and `#endif` directives.
Example:
#define DEBUG_MODE
#ifdef DEBUG_MODE
// Code to be compiled only in debug mode
#else
// Code to be compiled only if DEBUG_MODE is not defined
#endif
This example demonstrates how you can use macros to control the parts of code that are being compiled, depending on the presence or absence of a macro definition.
Macros in C++ are preprocessing directives that instruct the preprocessor to perform text substitutions before compilation. Defined using `#define`, they enable you to create symbolic constants, function-like constructs, and control conditional compilation. Macros can help in code optimization and customization, but it's essential to use them judiciously due to potential debugging and scope issues.
@@ -1,56 +1,3 @@
# Makefile
A Makefile is a configuration file used by the `make` utility to automate the process of compiling and linking code in a C++ project. It consists of a set of rules and dependencies that help in building the target executable or library from source code files.
Makefiles help developers save time, reduce errors, and ensure consistency in the build process. They achieve this by specifying the dependencies between different source files, and providing commands that generate output files (such as object files and executables) from input files (such as source code and headers).
Structure of a Makefile
-----------------------
A typical Makefile has the following structure:
* **Variables**: Define variables to store commonly used values, such as compiler flags, directories, or target names.
* **Rules**: Define how to generate output files from input files using a set of commands. Each rule has a _target_, a set of _prerequisites_, and a _recipe_.
* **Phony targets**: Targets that do not represent actual files in the project but serve as a way to group related rules and invoke them using a single command.
Example
-------
Consider a basic C++ project with the following directory structure:
project/
|-- include/
| |-- header.h
|-- src/
| |-- main.cpp
|-- Makefile
A simple Makefile for this project could be as follows:
# Variables
CXX = g++
CXXFLAGS = -Wall -Iinclude
SRC = src/main.cpp
OBJ = main.o
EXE = my_program
# Rules
$(EXE): $(OBJ)
$(CXX) $(CXXFLAGS) -o $(EXE) $(OBJ)
$(OBJ): $(SRC)
$(CXX) $(CXXFLAGS) -c $(SRC)
# Phony targets
.PHONY: clean
clean:
rm -f $(OBJ) $(EXE)
With this Makefile, you can simply run `make` in the terminal to build the project, and `make clean` to remove the output files. The Makefile specifies the dependencies between the source code, object files, and the final executable, as well as the commands to compile and link them.
Summary
-------
Makefiles provide a powerful way to automate building C++ projects using the `make` utility. They describe the dependencies and commands required to generate output files from source code, saving time and ensuring consistency in the build process.
A Makefile is a configuration file used by the `make` utility to automate the process of compiling and linking code in a project. It defines a set of rules and dependencies that specify how to build the final executable or library from source code. It consists of variables, rules with targets, prerequisites, recipes, and phony targets, which do not represent actual files, but execute related actions. Makefiles streamline the build process, reducing errors and ensuring consistency by specifying dependencies between source files and the commands to generate output files, such as object files and executables.
@@ -1 +1,3 @@
# undefined
# Memory Leakage
Memory leakage occurs when dynamically allocated memory is no longer accessible by the program, but the program fails to release it back to the operating system. This results in the memory being unusable by other parts of the program or other applications, gradually reducing the available memory and potentially leading to performance degradation or even program crashes.
@@ -1,52 +1,3 @@
# Memory Model in C++
# Memory Model
The memory model in C++ defines how the program stores and accesses data in computer memory. It consists of different segments, such as the Stack, Heap, Data and Code segments. Each of these segments is used to store different types of data and has specific characteristics.
Stack Memory
------------
Stack memory is used for automatic storage duration variables, such as local variables and function call data. Stack memory is managed by the compiler, and it's allocation and deallocation are done automatically. The stack memory is also a LIFO (Last In First Out) data structure, meaning that the most recent data allocated is the first to be deallocated.
void functionExample() {
int x = 10; // x is stored in the stack memory
}
Heap Memory
-----------
Heap memory is used for dynamic storage duration variables, such as objects created using the `new` keyword. The programmer has control over the allocation and deallocation of heap memory using `new` and `delete` operators. Heap memory is a larger pool of memory than the stack, but has a slower access time.
void functionExample() {
int* p = new int; // dynamically allocated int in heap memory
*p = 10;
// more code
delete p; // deallocate memory
}
Data Segment
------------
The Data segment is composed of two parts: the initialized data segment and the uninitialized data segment. The initialized data segment stores global, static, and constant variables with initial values, whereas the uninitialized segment stores uninitialized global and static variables.
// Initialized data segment
int globalVar = 10; // global variables
static int staticVar = 10; // static local variables
const int constVar = 10; // constant variables with value
// Uninitialized data segment
int globalVar; // uninitialized global variables
Code Segment
------------
The Code segment (also known as the Text segment) stores the executable code (machine code) of the program. It's usually located in a read-only area of memory to prevent accidental modification.
void functionExample() {
// The machine code for this function is stored in the code segment.
}
In summary, understanding the memory model in C++ helps to optimize the usage of memory resources and improves overall program performance.
The C++ memory model defines how a program organizes and manages memory during execution. It divides memory into different segments, each serving a specific purpose: the stack for function calls and local variables, the heap for dynamic memory allocation, the data segment for global and static variables, and the code segment for executable instructions. Understanding this model is crucial for writing efficient and bug-free C++ code, especially when dealing with memory management and resource allocation.
@@ -1,77 +1,3 @@
# Multiple Inheritance
Multiple inheritance is a feature in C++ where a class can inherit characteristics (data members and member functions) from more than one parent class. The concept is similar to single inheritance (where a class inherits from a single base class), but in multiple inheritance, a class can have multiple base classes.
When a class inherits multiple base classes, it becomes a mixture of their properties and behaviors, and can override or extend them as needed.
Syntax
------
Here is the syntax to declare a class with multiple inheritance:
class DerivedClass : access-specifier BaseClass1, access-specifier BaseClass2, ...
{
// class body
};
The `DerivedClass` will inherit members from both `BaseClass1` and `BaseClass2`. The `access-specifier` (like `public`, `protected`, or `private`) determines the accessibility of the inherited members.
Example
-------
Here is an example of multiple inheritance in action:
#include <iostream>
// Base class 1
class Animal
{
public:
void eat()
{
std::cout << "I can eat!\n";
}
};
// Base class 2
class Mammal
{
public:
void breath()
{
std::cout << "I can breathe!\n";
}
};
// Derived class inheriting from both Animal and Mammal
class Dog : public Animal, public Mammal
{
public:
void bark()
{
std::cout << "I can bark! Woof woof!\n";
}
};
int main()
{
Dog myDog;
// Calling members from both base classes
myDog.eat();
myDog.breath();
// Calling a member from the derived class
myDog.bark();
return 0;
}
Note
----
In some cases, multiple inheritance can lead to complications such as ambiguity and the "diamond problem". Ensure that you use multiple inheritance judiciously and maintain well-structured and modular classes to prevent issues.
For more information on C++ multiple inheritance and related topics, refer to C++ documentation or a comprehensive C++ programming guide.
Multiple inheritance in C++ allows a class to inherit from multiple base classes, combining their properties and behaviors into a single derived class. This means a class can inherit data members and member functions from several parent classes, offering a way to create more complex and specialized classes. However, it's essential to use multiple inheritance carefully, as it can introduce complexities like ambiguity and the diamond problem, requiring a good understanding of class hierarchies and potential conflict resolution.
@@ -1,77 +1,3 @@
# Multithreading in C++
# Multithreading
Multithreading is the concurrent execution of multiple threads within a single process or program. It improves the performance and efficiency of an application by allowing multiple tasks to be executed in parallel.
In C++, multithreading support is available through the `thread` library introduced in the C++11 standard.
Basic Thread Creation
---------------------
To create a new thread, include the `<thread>` header file and create an instance of `std::thread` that takes a function as an argument. The function will be executed in a new thread.
#include <iostream>
#include <thread>
void my_function() {
std::cout << "This function is executing in a separate thread\n";
}
int main() {
std::thread t(my_function);
t.join(); // waits for the thread to complete
return 0;
}
Thread with Arguments
---------------------
You can pass arguments to the thread function by providing them as additional arguments to the `std::thread` constructor.
#include <iostream>
#include <thread>
void print_sum(int a, int b) {
std::cout << "The sum is: " << a + b << '\n';
}
int main() {
std::thread t(print_sum, 3, 5);
t.join();
return 0;
}
Mutex and Locks
---------------
When multiple threads access shared resources, there is a possibility of a data race. To avoid this, use mutex and locks to synchronize shared resource access.
#include <iostream>
#include <mutex>
#include <thread>
std::mutex mtx;
void print_block(int n, char c) {
{
std::unique_lock<std::mutex> locker(mtx);
for (int i = 0; i < n; ++i) {
std::cout << c;
}
std::cout << '\n';
}
}
int main() {
std::thread t1(print_block, 50, '*');
std::thread t2(print_block, 50, '$');
t1.join();
t2.join();
return 0;
}
This short introduction should help you get started with basic multithreading techniques in C++. There is a lot more to learn, such as thread pools, condition variables, and atomic operations for advanced synchronization and performance tuning.
Multithreading allows concurrent execution of multiple threads within a single process, enhancing application performance by enabling parallel task execution. C++ provides multithreading support through the `<thread>` library (introduced in C++11), enabling thread creation, argument passing, and synchronization mechanisms like mutexes and locks to manage shared resource access and prevent data races. This introduction covers basic thread creation, passing arguments to threads, and using mutexes for thread synchronization; more advanced topics such as thread pools, condition variables, and atomic operations exist for advanced synchronization and performance tuning.
@@ -1,25 +1,3 @@
# Name Mangling
Name mangling, also known as name decoration, is a technique used by compilers to encode additional information about the scope, type, linkage, or other identifying information of an identifier (function names, variable names, etc.) within its name. The primary purpose of name mangling is to support function overloading, which allows multiple functions with the same name but different parameter lists to coexist in a single program.
In C++, the compiler generates a mangled name for each function and variable based on their scopes and types. The mangled name is usually formed by concatenating the original name, parameter types, and other information, often using a prefix or suffix.
For example, suppose you have the following function:
int add(int a, int b)
{
return a + b;
}
The compiler might generate a mangled name such as `_Z3addii`, which encodes the function name `add` and its two `int` parameters.
The exact rules for name mangling are implementation and platform dependent. Different compilers may mangle names differently, which can lead to incompatibilities when attempting to link together object files or libraries compiled with different compilers.
Some tools, such as c++filt (included in GCC and Clang), can be used to demangle a mangled name back to the original identifier, which can be useful when debugging or working with symbol tables.
$ echo "_Z3addii" | c++filt
add(int, int)
In general, it is not necessary for you to understand the details of name mangling when writing code in C++, as the compiler handles it automatically. However, it can affect program behavior in some cases, such as when using external libraries or linking object files from different compilers.
Name mangling, also known as name decoration, is a technique compilers use to encode extra information like scope, type, and linkage into identifier names (like function and variable names). This allows C++ to support function overloading, where multiple functions can share the same name but have different parameters. The compiler generates a mangled name based on these details, though the exact mangling rules vary between compilers and platforms. Tools like `c++filt` can demangle these names back to their original form, which is useful for debugging. While you usually don't need to understand the details of name mangling, it can be important when working with external libraries or linking object files from different compilers.
@@ -1,101 +1,3 @@
# Namespaces in C++
# Namespaces
In C++, a namespace is a named scope or container that is used to organize and enclose a collection of code elements, such as variables, functions, classes, and other namespaces. They are mainly used to divide and manage the code base, giving developers control over name collisions and the specialization of code.
Syntax
------
Here's the syntax for declaring a namespace:
namespace identifier {
// code elements
}
Using Namespaces
----------------
To access elements within a namespace, you can use the scope resolution operator `::`. Here are some examples:
### Declaring and accessing a namespace
#include <iostream>
namespace animals {
std::string dog = "Bobby";
std::string cat = "Lilly";
}
int main() {
std::cout << "Dog's name: " << animals::dog << '\n';
std::cout << "Cat's name: " << animals::cat << '\n';
return 0;
}
### Nesting namespaces
Namespaces can be nested within other namespaces:
#include <iostream>
namespace outer {
int x = 10;
namespace inner {
int y = 20;
}
}
int main() {
std::cout << "Outer x: " << outer::x << '\n';
std::cout << "Inner y: " << outer::inner::y << '\n';
return 0;
}
`using` Keyword
---------------
You can use the `using` keyword to import namespaced elements into the current scope. However, this might lead to name conflicts if multiple namespaces have elements with the same name.
### Using a single element from a namespace
#include <iostream>
namespace animals {
std::string dog = "Bobby";
std::string cat = "Lilly";
}
int main() {
using animals::dog;
std::cout << "Dog's name: " << dog << '\n';
return 0;
}
### Using the entire namespace
#include <iostream>
namespace animals {
std::string dog = "Bobby";
std::string cat = "Lilly";
}
int main() {
using namespace animals;
std::cout << "Dog's name: " << dog << '\n';
std::cout << "Cat's name: " << cat << '\n';
return 0;
}
In conclusion, namespaces are a useful mechanism in C++ to organize code, avoid naming conflicts, and manage the visibility of code elements.
Namespaces in C++ provide a way to organize code into logical groups, preventing naming conflicts when using code from different libraries or parts of a large project. They act as containers for variables, functions, classes, and even other namespaces. You can access elements within a namespace using the scope resolution operator `::`, nest namespaces for further organization, and selectively import elements or entire namespaces into the current scope using the `using` keyword to simplify code while being mindful of potential name collisions.
@@ -1 +1,3 @@
# undefined
# New/Delete Operators
The `new` and `delete` operators in C++ are used for dynamic memory allocation and deallocation. `new` allocates a block of memory on the heap and returns a pointer to the beginning of that block. `delete` then releases the memory block previously allocated by `new`, making it available for other uses. Proper use of `new` and `delete` is crucial to prevent memory leaks.
@@ -1,32 +1,3 @@
# Ninja
Ninja is a small build system with a focus on speed. It is designed to handle large projects by generating build files that implement the minimal amount of work necessary to build the code. This results in faster build times, especially for large codebases. Ninja is often used in conjunction with other build systems like CMake, which can generate Ninja build files for you.
Ninja build files are typically named `build.ninja` and contain rules, build statements, and variable declarations. Here's a simple example of a Ninja build file for a C++ project:
# Variable declarations
cxx = g++
cflags = -Wall -Wextra -std=c++17
# Rule for compiling the C++ files
rule cxx_compile
command = $cxx $cflags -c $in -o $out
# Build statements for the source files
build main.o: cxx_compile main.cpp
build foo.o: cxx_compile foo.cpp
# Rule for linking the object files
rule link
command = $cxx $in -o $out
# Build statement for the final executable
build my_program: link main.o foo.o
To build the project using this `build.ninja` file, simply run `ninja` in the terminal:
$ ninja
This will build the `my_program` executable by first compiling the `main.cpp` and `foo.cpp` files into object files, and then linking them together.
Ninja is a small, fast build system designed for speed and efficiency. Instead of directly interpreting project structure, it executes pre-generated build plans. This allows it to build only what's necessary, leading to significantly faster build times, particularly in large projects. It's commonly used with meta-build systems like CMake, which generate the `build.ninja` files that Ninja then uses to perform the actual build process.
@@ -1,29 +1,3 @@
# Non-Copyable
# Non-Copyable / Non-Moveable
The non-copyable idiom is a C++ design pattern that prevents objects from being copied or assigned. It's usually applied to classes that manage resources, like file handles or network sockets, where copying the object could cause issues like resource leaks or double deletions.
To make a class non-copyable, you need to delete the copy constructor and the copy assignment operator. This can be done explicitly in the class declaration, making it clear to other programmers that copying is not allowed.
Here's an example of how to apply the non-copyable idiom to a class:
class NonCopyable {
public:
NonCopyable() = default;
~NonCopyable() = default;
// Delete the copy constructor
NonCopyable(const NonCopyable&) = delete;
// Delete the copy assignment operator
NonCopyable& operator=(const NonCopyable&) = delete;
};
To use the idiom, simply inherit from the `NonCopyable` class:
class MyClass : private NonCopyable {
// MyClass is now non-copyable
};
This ensures that any attempt to copy or assign objects of `MyClass` will result in a compilation error, thus preventing unwanted behavior.
The non-copyable/non-moveable idiom in C++ prevents objects of a class from being copied or moved. This is achieved by deleting the copy constructor, copy assignment operator, move constructor, and move assignment operator. It's useful for classes that manage exclusive resources, ensuring that only one instance controls the resource at a time, preventing issues like resource duplication or double deletion. By disabling copying and moving, you enforce a unique ownership model for instances of the class.
@@ -1,42 +1,3 @@
# NuGet
[NuGet](https://www.nuget.org/) is a Microsoft-supported package manager for the .NET framework, mainly used in C# and other .NET languages, but also supports C++ projects with `PackageReference`. It allows you to easily add, update, and manage dependencies in your projects.
### Installation
You can use NuGet either as a command-line tool or integrated in your preferred IDE like Visual Studio or Visual Studio Code. If you're using Visual Studio, it comes pre-installed. For other editors, you may need to download the command-line tool `nuget.exe`.
### Usage
You can use NuGet to manage your C++ dependencies using the PackageReference format in vcxproj files:
* Tools > NuGet Package Manager > Manage NuGet Packages for Solution…
* Package source should be set to "[nuget.org](http://nuget.org)"
* Select the Projects tab
* Use the search box to find packages
For example, to install a package called "PackageName" for all configurations:
<Project>
<ItemGroup>
<PackageReference Include="PackageName" Version="1.0.0" />
</ItemGroup>
...
</Project>
### NuGet Command-Line
You can also use the command-line tool `nuget.exe` for more advanced scenarios or for specific needs.
Here's an example of installing a package using the command line:
nuget install PackageName
And updating a package:
nuget update PackageName
For more information and detailed examples on using NuGet in your projects, please refer to the [official documentation](https://docs.microsoft.com/en-us/nuget/guides/native-packages).
NuGet is a package manager initially designed for the .NET ecosystem, but it also extends its functionality to C++ projects through the `PackageReference` format. It simplifies the process of adding, updating, and managing external libraries and dependencies within your C++ projects, whether you're using Visual Studio's GUI, its integrated command-line tools, or the standalone `nuget.exe` executable. With NuGet, managing dependencies becomes more streamlined and efficient, allowing you to focus on your core C++ code.
@@ -1,117 +1,3 @@
# Object-Oriented Programming (OOP) in C++
# Object-Oriented Programming (OOP)
Object-oriented programming (OOP) is a programming paradigm that uses objects, which are instances of classes, to perform operations and interact with each other. In C++, you can achieve OOP through the use of classes and objects.
Classes
-------
A class is a blueprint for creating objects. It defines the structure (data members) and behavior (member functions) for a type of object. Here's an example of a simple class:
class Dog {
public:
std::string name;
int age;
void bark() {
std::cout << name << " barks!\n";
}
};
This `Dog` class has two data members: `name` and `age`, and one member function `bark`. You can create an object of this class and access its members like this:
Dog myDog;
myDog.name = "Fido";
myDog.age = 3;
myDog.bark(); // Output: Fido barks!
Encapsulation
-------------
Encapsulation is the concept of bundling data and functions that operate on that data within a single unit, such as a class. It helps to hide the internal implementation details of a class and expose only the necessary information and functionalities. In C++, you can use access specifiers like `public`, `private`, and `protected` to control the visibility and accessibility of class members. For example:
class Dog {
private:
std::string name;
int age;
public:
void setName(std::string n) {
name = n;
}
void setAge(int a) {
age = a;
}
void bark() {
std::cout << name << " barks!\n";
}
};
In this example, we've made the `name` and `age` data members `private` and added public member functions `setName` and `setAge` to modify them. This way, the internal data of the `Dog` class is protected and only accessible through the provided functions.
Inheritance
-----------
Inheritance is the concept of deriving new classes from existing ones, which enables code reusability and organization. In C++, inheritance is achieved by using a colon `:` followed by the base class' access specifier and the base class name. For example:
class Animal {
public:
void breathe() {
std::cout << "I can breathe\n";
}
};
class Dog : public Animal {
public:
void bark() {
std::cout << "Dog barks!\n";
}
};
In this example, the `Dog` class inherits from the `Animal` class, so the `Dog` class can access the `breathe` function from the `Animal` class. When you create a `Dog` object, you can use both `breathe` and `bark` functions.
Dog myDog;
myDog.breathe(); // Output: I can breathe
myDog.bark(); // Output: Dog barks!
Polymorphism
------------
Polymorphism allows you to use a single interface to represent different types. In C++, it's mainly achieved using function overloading, virtual functions, and overriding. For example:
class Animal {
public:
virtual void makeSound() {
std::cout << "The Animal makes a sound\n";
}
};
class Dog : public Animal {
public:
void makeSound() override {
std::cout << "Dog barks!\n";
}
};
class Cat : public Animal {
public:
void makeSound() override {
std::cout << "Cat meows!\n";
}
};
In this example, we have an `Animal` base class with a virtual `makeSound` function. We then derive two classes, `Dog` and `Cat`, which override the `makeSound` function. This enables polymorphic behavior, where an `Animal` pointer or reference can be used to access the correct `makeSound` function depending on the derived class type.
Animal *animals[2] = {new Dog, new Cat};
animals[0]->makeSound(); // Output: Dog barks!
animals[1]->makeSound(); // Output: Cat meows!
That's a brief overview of object-oriented programming concepts in C++.
Object-oriented programming is a style of programming centered around "objects," which combine data (attributes) and functions (methods) that operate on that data. OOP enables you to create modular, reusable, and maintainable code by organizing your programs around these objects and their interactions. Key concepts in OOP include classes (blueprints for objects), encapsulation (bundling data and methods), inheritance (creating new classes from existing ones), and polymorphism (using a single interface for different types).
@@ -1 +1,3 @@
# undefined
# OpenCL
OpenCL (Open Computing Language) is a framework for writing programs that execute across heterogeneous platforms consisting of CPUs, GPUs, and other processors. It allows developers to harness the parallel processing power of different types of hardware using a unified API, enabling acceleration of computationally intensive tasks. With OpenCL, you can write code that can run on a variety of devices, making it a versatile tool for parallel programming.
@@ -1 +1,3 @@
# undefined
# OpenCV
OpenCV (Open Source Computer Vision Library) is a comprehensive collection of programming functions primarily aimed at real-time computer vision. It encompasses a vast array of algorithms for image processing, object detection, video analysis, and machine learning, providing developers with tools to analyze and manipulate visual data.
@@ -1 +1,3 @@
# undefined
# Orbit Profiler
Orbit Profiler is a performance analysis tool designed to help developers identify bottlenecks and optimize the performance of their applications. It provides detailed insights into CPU usage, memory allocation, and other performance metrics through sampling and tracing. This helps pinpoint areas where code can be made more efficient.
@@ -1,50 +1,3 @@
# Package Managers
Package managers are tools that automate the process of installing, upgrading, and managing software (libraries, frameworks, and other dependencies) for a programming language, such as C++.
Some popular package managers used in the C++ ecosystem include:
* **Conan**
* **vcpkg**
* **C++ Archive Network (cppan)**
Conan
-----
[Conan](https://conan.io/) is an open-source, decentralized, cross-platform package manager for C and C++ developers. It simplifies managing dependencies and reusing code, which benefits multi-platform development projects.
For example, installing a library using Conan:
conan install poco/1.9.4@
vcpkg
-----
[vcpkg](https://github.com/microsoft/vcpkg) is a cross-platform package manager created by Microsoft. It is an open-source library management system for C++ developers to build and manage their projects.
For example, installing a package using vcpkg:
./vcpkg install boost:x64-windows
C++ Archive Network (cppan)
---------------------------
[cppan](https://cppan.org/) is a package manager and software repository for C++ developers, simplifying the process of managing and distributing C++ libraries and tools. It's now part of [build2](https://build2.org/), a build toolchain that provides a package manager.
An example of a `cppan.yml` file:
#
# cppan.yml
#
project:
api_version: 1
depend:
- pvt.cppan.demo.sqlite3
- pvt.cppan.demo.xz_utils.lzma
With these package managers, you can streamline your development process and easily manage dependencies in your C++ projects. In addition, you can easily reuse the code in your projects to improve code quality and accelerate development.
Package managers are essential tools that streamline the process of incorporating external libraries and dependencies into C++ projects. They automate the tasks of installing, updating, and managing these components, simplifying development and promoting code reuse. Popular options in the C++ ecosystem include Conan, a decentralized, cross-platform solution; vcpkg, a package manager developed by Microsoft; and cppan, which is now part of the build2 build toolchain. These tools help developers efficiently manage dependencies, improving code quality and accelerating development.
@@ -1,57 +1,3 @@
# Partial Template Specialization
Partial template specialization is a concept in C++ templates, which allows you to specialize a template for a subset of its possible type arguments. It is particularly useful when you want to provide a customized implementation for a particular group of types without having to define separate specializations for all types in that group.
Partial template specialization is achieved by providing a specialization of a template with a new set of template parameters. This new template will be chosen when the compiler deduces the types that match the partial specialization.
Here is a code example that demonstrates partial template specialization:
// Primary template
template <typename T>
struct MyTemplate {
static const char* name() {
return "General case";
}
};
// Partial specialization for pointers
template <typename T>
struct MyTemplate<T*> {
static const char* name() {
return "Partial specialization for pointers";
}
};
// Full specialization for int
template <>
struct MyTemplate<int> {
static const char* name() {
return "Full specialization for int";
}
};
int main() {
MyTemplate<double> t1; // General case
MyTemplate<double*> t2; // Partial specialization for pointers
MyTemplate<int> t3; // Full specialization for int
std::cout << t1.name() << '\n';
std::cout << t2.name() << '\n';
std::cout << t3.name() << '\n';
return 0;
}
In the example above, we have defined a primary template `MyTemplate` with a single type parameter `T`. We then provide a partial template specialization for pointer types by specifying `MyTemplate<T*>`. This means that the partial specialization will be chosen when the type argument is a pointer type.
Lastly, we provide a full specialization for the `int` type by specifying `MyTemplate<int>`. This will be chosen when the type argument is `int`.
When running this example, the output will be:
General case
Partial specialization for pointers
Full specialization for int
This demonstrates that the partial specialization works as expected, and is chosen for pointer types, while the full specialization is chosen for the `int` type.
Partial template specialization in C++ allows you to create specialized versions of a template for specific subsets of types. This is achieved by providing a new template definition that is selected by the compiler when the template arguments match the specified criteria. It offers a way to customize template behavior for certain type categories, like pointers or specific data types, without needing complete specializations for every possible type.
@@ -1,50 +1,3 @@
# Pimpl Idiom
Pimpl (Pointer-to-Implementation) idiom, also known as a private class data, compiler firewall, or handle classes, is a technique used in C++ to hide the implementation details of a class by using a forward declaration to a private structure or class, keeping the public interface of the class clean, and reducing compile-time dependencies.
Implementation
--------------
Here is a simple example illustrating the Pimpl idiom:
**my\_class.h**
class MyClass_Impl; // forward declaration
class MyClass
{
public:
MyClass();
~MyClass();
void some_method();
private:
MyClass_Impl *pimpl; // pointer to the implementation
};
**my\_class.cpp**
#include "my_class.h"
#include <iostream>
class MyClass_Impl // the actual implementation
{
public:
void some_method()
{
std::cout << "Implementation method called!\n";
}
};
MyClass::MyClass() : pimpl(new MyClass_Impl()) {} // constructor
MyClass::~MyClass() { delete pimpl; } // destructor
void MyClass::some_method()
{
pimpl->some_method(); // delegation to the implementation
}
Now, all the public methods of `MyClass` will delegate the calls to the corresponding methods of `MyClass_Impl`. By doing this, you can hide the details of class implementation, reduce the compile-time dependencies, and ease the maintenance of your code.
The Pimpl (Pointer to Implementation) idiom is a C++ technique used to hide the implementation details of a class. This is achieved by declaring a private implementation class and holding a pointer to it within the main class. The public interface remains clean, and compile-time dependencies are significantly reduced. This promotes better code organization, reduces recompilation times, and improves binary compatibility.
@@ -1 +1,3 @@
# undefined
# POCO
POCO (Portable Components) is a C++ class library that simplifies network-centric, portable applications. It offers a comprehensive set of tools and components for tasks such as networking, data processing, and basic utilities. POCO focuses on being modular and cross-platform.
@@ -1,163 +1,6 @@
# Pointers
# Pointers and References
A pointer is a variable that stores the memory address of another variable (or function). It points to the location of the variable in memory, and it allows you to access or modify the value indirectly. Here's a general format to declare a pointer:
dataType *pointerName;
**Initializing a pointer:**
int num = 10;
int *ptr = &num; // Pointer 'ptr' now points to the memory address of 'num'
**Accessing value using a pointer:**
int value = *ptr; // Value now contains the value of the variable that 'ptr' points to (i.e., 10)
**Function pointer:**
int add(int a, int b)
{
return a + b;
}
int main()
{
int (*funcptr) (int, int) = add; // Pointer 'funcptr' now points to the functions 'add'
funcptr(4, 5); // Return 9
}
References
----------
A reference is an alias for an existing variable, meaning it's a different name for the same memory location. Unlike pointers, references cannot be null, and they must be initialized when they are declared. Once a reference is initialized, it cannot be changed to refer to another variable.
Here's a general format to declare a reference:
dataType &referenceName = existingVariable;
**Example:**
int num = 10;
int &ref = num; // Reference 'ref' is now an alias of 'num'
Modifying the value of `ref` will also modify the value of `num` because they share the same memory location.
**Note:** References are generally used when you want to pass a variable by reference in function arguments or when you want to create an alias for a variable without the need for pointer syntax.
Constant Pointers and Pointers to Constants in C++
==================================================
In C++, the placement of the `const` keyword in a pointer declaration changes its meaning. Let’s go through the three main cases.
* * *
1\. Constant Pointer
--------------------
A **constant pointer** means the pointer itself cannot change the address it holds after initialization, but the value at that address can still be modified.
**Syntax:**
int x = 10;
int y = 20;
int* const ptr = &x; // Constant pointer to int
*ptr = 15; // ✅ Allowed: we can change the value of x
// ptr = &y; // ❌ Error: cannot make ptr point to y
👉 **Rule:**
* You **cannot change** what it points to.
* You **can change** the value at the address.
* * *
2\. Pointer to Constant
-----------------------
A **pointer to constant** means the pointer can change to point to different addresses, but the value at the address cannot be modified through this pointer.
**Syntax:**
int x = 10;
int y = 20;
const int* ptr = &x; // Pointer to constant int
// *ptr = 15; // ❌ Error: cannot modify x through ptr
ptr = &y; // ✅ Allowed: can point to another variable
👉 **Rule:**
* You **can change** what it points to.
* You **cannot change** the value at the address through this pointer.
* * *
3\. Constant Pointer to Constant
--------------------------------
A **constant pointer to constant** means neither the pointer nor the value it points to can be changed.
**Syntax:**
int x = 10;
int y = 20;
const int* const ptr = &x; // Constant pointer to constant int
// *ptr = 15; // ❌ Error: cannot modify x
// ptr = &y; // ❌ Error: cannot make ptr point to y
👉 **Rule:**
* You **cannot change** what it points to.
* You **cannot change** the value at the address through this pointer.
* * *
Summary Table
-------------
Declaration
Can change pointer?
Can change value?
`int* const ptr`
❌ No
✅ Yes
`const int* ptr`
✅ Yes
❌ No
`const int* const ptr`
❌ No
❌ No
* * *
Rule of Thumb
-------------
* `int* const ptr` → constant **pointer**, mutable **pointee**.
* `const int* ptr` → mutable **pointer**, constant **pointee**.
* `const int* const ptr` → constant **pointer**, constant **pointee**.
Pointers and references are fundamental concepts in C++ that allow indirect access to variables. A pointer stores the memory address of another variable, enabling dynamic memory management and manipulation. A reference, on the other hand, acts as an alias for an existing variable, providing a direct way to access and modify its value. Understanding the difference between pointers and references, including constant pointers and pointers to constants, is crucial for writing efficient and safe C++ code.
Visit the following resources to learn more:
@@ -1 +1,3 @@
# undefined
# Protobuf
Protobuf (Protocol Buffers) is a language-neutral, platform-neutral, extensible mechanism for serializing structured data. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams, using a variety of languages. Think of it as a more efficient and flexible alternative to XML or JSON.
@@ -1 +1,3 @@
# undefined
# pybind11
pybind11 is a lightweight header-only library that allows you to create Python bindings for your existing C++ code. It essentially lets you expose C++ functions, classes, and data structures to Python, enabling seamless interoperability between the two languages. This allows you to leverage the performance of C++ while utilizing the flexibility and ease of use of Python.
@@ -1 +1,3 @@
# undefined
# PyTorch C++
PyTorch C++ provides a front-end for the PyTorch machine learning framework, allowing you to build and deploy models using C++ instead of Python. This enables you to leverage the performance and control of C++ for computationally intensive tasks, especially in production environments where low latency and resource efficiency are critical. It involves using a C++ API to define and execute PyTorch operations.
@@ -1 +1,3 @@
# undefined
# Qt
Qt is a cross-platform application development framework widely used for creating graphical user interfaces (GUIs) and applications that run on various operating systems, such as Windows, macOS, Linux, and embedded systems. It provides a set of tools and libraries that simplify the development process, offering features like widgets, networking, database access, and multimedia support. Qt uses C++ as its primary programming language, extending it with its own meta-object compiler (moc) and signal/slot mechanism for event handling.
@@ -1,70 +1,3 @@
# RAII (Resource Acquisition Is Initialization)
# RAII
RAII is a popular idiom in C++ that focuses on using the object's life cycle to manage resources. It encourages binding the resource lifetime to the scope of a corresponding object so that it's automatically acquired when an object is created and released when the object is destroyed. This helps in simplifying the code, avoiding leaks and managing resources efficiently.
Code Examples
-------------
Here's an example of using RAII to manage resources, specifically a dynamically allocated array:
class ManagedArray {
public:
ManagedArray(size_t size) : size_(size), data_(new int[size]) {
}
~ManagedArray() {
delete[] data_;
}
// Access function
int& operator [](size_t i) {
return data_[i];
}
private:
size_t size_;
int* data_;
};
Usages:
{
ManagedArray arr(10);
arr[0] = 42;
// No need to explicitly free memory, it will be automatically released when arr goes out of scope.
}
Another common use case is managing a mutex lock:
class Lock {
public:
Lock(std::mutex& mtx) : mutex_(mtx) {
mutex_.lock();
}
~Lock() {
mutex_.unlock();
}
private:
std::mutex& mutex_;
};
Usages:
std::mutex some_mutex;
void protected_function() {
Lock lock(some_mutex);
// Do some work that must be synchronized
// No need to explicitly unlock the mutex, it will be automatically unlocked when lock goes out of scope.
}
In both examples, the constructor acquires the resource (memory for the array and the lock for the mutex), and the destructor takes care of releasing them. This way, the resource management is tied to the object's lifetime, and the resource is correctly released even in case of an exception being thrown.
RAII (Resource Acquisition Is Initialization) is a C++ idiom that ties the management of resources to the lifetime of objects. Resources are acquired during object construction and automatically released when the object is destroyed, typically in the destructor. This ensures resources are properly managed, even in the face of exceptions, preventing leaks and simplifying code by automating resource cleanup.
@@ -1 +1,3 @@
# undefined
# Ranges v3
Ranges v3 is a modern C++ library that provides tools for working with sequences of elements in a more composable and expressive way. It introduces concepts like views and algorithms that operate on ranges, allowing you to chain operations together to process data efficiently and elegantly without relying heavily on iterators. Ranges v3 simplifies common data manipulation tasks such as filtering, transforming, and sorting by offering a higher-level abstraction over traditional iterator-based code.
@@ -1 +1,3 @@
# Raw Pointers
# Raw Pointers
Raw pointers are variables that store the memory address of another variable. They directly hold the location in memory where a value is stored, allowing you to access and manipulate that value. Using raw pointers requires careful memory management as you are responsible for allocating and deallocating the memory they point to.
@@ -1,115 +1,7 @@
# References
A reference can be considered as a constant pointer (not to be confused with a pointer to a constant value) which always points to (references) the same object. They are declared using the `&` (ampersand) symbol.
In C++, a reference is an alias for an existing variable. It provides an alternative name to access the same memory location. References are similar to pointers in some ways, but they have key differences: a reference must be initialized when it's created, and once initialized, it cannot be changed to refer to a different variable. References are commonly used to pass arguments to functions by reference, allowing the function to modify the original variable.
Declaration and Initialization
------------------------------
Visit the following resources to learn more:
To declare a reference, use the variable type followed by the `&` symbol and the reference's name. Note that you must initialize a reference when you declare it.
int var = 10; // Declare an integer variable
int& ref = var; // Declare a reference that "points to" var
Usage
-----
You can use the reference just like you'd use the original variable. When you change the value of the reference, the value of the original variable also changes, because they both share the same memory location.
var = 20; // Sets the value of var to 20
std::cout << ref << '\n'; // Outputs 20
ref = 30; // Sets the value of ref to 30
std::cout << var << '\n'; // Outputs 30
Function Parameters
-------------------
You can use references as function parameters to create an alias for an argument. This is commonly done when you need to modify the original variable or when passing an object of considerable size to avoid the cost of copying.
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 5, y = 10;
std::cout << "Before Swap: x = " << x << " y = " << y << '\n'; // Outputs 5 10
swap(x, y);
std::cout << "After Swap: x = " << x << " y = " << y << '\n'; // Outputs 10 5
}
References in Range-based For Loops
-----------------------------------
When iterating over containers such as `std::vector<std::string>`, the choice of `auto`, `auto &`, `auto const &`, or `const auto` makes a big difference:
std::vector<std::string> stooges {"xyz", "abc", "def"};
// Read-only, no copies
for (auto const &str : stooges)
std::cout << str << std::endl;
// Makes a copy of each string
for (auto str : stooges)
std::cout << str << std::endl;
// Direct reference, can modify original elements
for (auto &str : stooges)
str += "!";
// Makes a copy of each string, but prevents modification of the copy
for (const auto str : stooges)
std::cout << str << std::endl;
### Comparison
| Loop style | Copies made? | Can modify element? | Efficiency | Typical use case |
|---------------------|--------------|---------------------|------------------------------------|------------------------------------------|
| `auto str` | ✅ Yes | ✅ Only the copy | Less efficient for large objects | When you need a local, mutable copy |
| `auto const &str` | ❌ No | ❌ Read-only | Most efficient for read-only use | Safely read elements without copying |
| `auto &str` | ❌ No | ✅ Modifies original | Efficient, mutates container | Modify elements in place |
| `const auto str` | ✅ Yes | ❌ Read-only | Less efficient for large objects | Explicit read-only copy |
Example: Why `const auto str` Can Be Useful
-------------------------------------------
Sometimes you want to make sure you **don’t accidentally modify** the loop variable, even though it’s just a copy. Declaring it `const` ensures this safety:
std::vector<std::string> stooges {"xyz", "abc", "def"};
for (const auto str : stooges) {
// str += "!"; // ❌ Error: str is const, cannot be modified
std::cout << str << std::endl; // Safe read-only copy
}
This is useful when:
* You **don’t need references** but still want to guarantee immutability inside the loop body.
* You want to make your **intent explicit**: the loop variable should not be changed.
* * *
Rule of Thumb
-------------
* Use **`auto const &`** for **read-only access** (best efficiency).
* Use **`auto &`** when you need to **modify elements in place**.
* Use **`auto`** when you need a **mutable copy**.
* Use **`const auto`** when you want a **read-only copy** inside the loop to prevent accidental modification.
* * *
Performance Notes
-----------------
* For **large objects** like `std::string`, `std::vector`, or custom classes, prefer **`auto const &`** to avoid unnecessary copies.
* For **small, cheap-to-copy types** like `int`, `char`, or `bool`, using `auto` or `const auto` is fine and often simpler.
* `auto &` should only be used when you _intend_ to modify the elements in place, since it directly mutates the container.
- [@article@References](https://en.cppreference.com/w/cpp/language/reference)
@@ -1,29 +1,3 @@
# reinterpret_cast
`reinterpret_cast` is a type of casting in C++ that allows you to change the type of a pointer or an integer without altering the representation of the data. It is generally used when the conversion required is too low-level or not supported by other casting methods, such as `static_cast`.
Using `reinterpret_cast` should be handled with care, as it can lead to undefined behavior and severe problems if used incorrectly.
Here's an example of usage:
#include <iostream>
int main() {
int num = 42;
int *num_ptr = &num;
// Disguise the integer pointer as a char pointer
char *char_ptr = reinterpret_cast<char *>(num_ptr);
for (size_t i = 0; i < sizeof(int); ++i) {
// Print the individual bytes of the integer as characters
std::cout << "Byte " << i << ": " << char_ptr[i] << '\n';
}
return 0;
}
In this example, we're using `reinterpret_cast` to change the type of a pointer from `int *` to `char *`, effectively treating the integer as an array of characters and printing each byte.
Remember that when using `reinterpret_cast`, you should be cautious about dereferencing the converted pointers. The behavior can be unpredictable, and it can lead to issues, such as accessing memory regions that are not intended to be accessed. `reinterpret_cast` should be used sparingly and only when a low-level conversion is necessary.
`reinterpret_cast` is a powerful but potentially dangerous type of casting operator in C++. It allows you to convert between unrelated pointer types, integer types, or between pointers and integers. Unlike other casts, it doesn't perform any type checking or data conversion. Its primary function is to reinterpret the bit pattern of an expression as a different type, making it useful for low-level operations, but also requiring extreme caution to avoid undefined behavior. Use it only when absolutely necessary and understand the underlying memory layout involved.
@@ -1,72 +1,3 @@
# Run-Time Type Identification (RTTI)
Run-Time Type Identification (RTTI) is a feature in C++ that allows you to obtain the type information of an object during program execution. This can be useful when using dynamic typing, where the type of an object can change at runtime.
There are two main mechanisms for RTTI in C++:
* `typeid` operator
* `dynamic_cast` operator
typeid operator
---------------
`typeid` is an operator that returns a reference to an object of type `std::type_info`, which contains information about the type of the object. The header file `<typeinfo>` should be included to use `typeid`.
Here is an example:
#include <iostream>
#include <typeinfo>
class Base { virtual void dummy() {} };
class Derived : public Base { /* ... */ };
int main() {
Base* base_ptr = new Derived;
// Using typeid to get the type of the object
std::cout << "Type: " << typeid(*base_ptr).name() << '\n';
delete base_ptr;
return 0;
}
dynamic\_cast operator
----------------------
`dynamic_cast` is a type-casting operator that performs a runtime type check and safely downcasts a base pointer or reference to a derived pointer or reference. It returns null or throws a bad\_cast exception (if casting references) when the casting fails.
Here is an example:
#include <iostream>
class Base { virtual void dummy() {} };
class Derived1 : public Base { /* ... */ };
class Derived2 : public Base { /* ... */ };
int main() {
Base* base_ptr = new Derived1;
// Using dynamic_cast to safely downcast the pointer
Derived1* derived1_ptr = dynamic_cast<Derived1*>(base_ptr);
if (derived1_ptr) {
std::cout << "Downcast to Derived1 successful\n";
}
else {
std::cout << "Downcast to Derived1 failed\n";
}
Derived2* derived2_ptr = dynamic_cast<Derived2*>(base_ptr);
if (derived2_ptr) {
std::cout << "Downcast to Derived2 successful\n";
}
else {
std::cout << "Downcast to Derived2 failed\n";
}
delete base_ptr;
return 0;
}
Please note that the use of RTTI can have some performance overhead, as it requires additional compiler-generated information to be stored and processed during runtime.
Run-Time Type Identification (RTTI) in C++ allows you to determine the type of an object during program execution. This is particularly useful when working with polymorphism and inheritance. C++ provides two primary mechanisms for RTTI: the `typeid` operator, which retrieves type information, and the `dynamic_cast` operator, which safely converts pointers or references between types at runtime, handling potential casting failures gracefully. While powerful, be mindful that RTTI can introduce some performance overhead due to the runtime checks involved.
@@ -1,106 +1,3 @@
# Rule of Zero, Five, Three
**Rule of Zero, Three, and Five in C++**
The Rule of Zero, Three, and Five is a set of guidelines for managing object resources in modern C++, related to structures and classes. These rules deal with the default behavior of constructors, destructors, and other special member functions that are necessary for proper resource management.
**Rule of Zero**
The Rule of Zero states that if a class or structure does not explicitly manage resources, it should not define any of the special member functions, i.e., destructor, copy constructor, copy assignment operator, move constructor, and move assignment operator. The compiler will automatically generate these functions, and the behavior will be correct for managing resources like memory and file handles.
_Example:_
struct MyResource {
std::string name;
int value;
};
In this example, MyResource is a simple structure that does not manage any resources, so it does not define any special member functions. The compiler will generate them automatically, and the behavior will be correct.
**Rule of Three**
The Rule of Three states that a class or structure that manages resources should define the following three special member functions:
* Destructor
* Copy constructor
* Copy assignment operator
These functions are necessary for proper resource management, such as releasing memory or correctly handling deep copies.
_Example:_
class MyResource {
public:
// Constructor and destructor
MyResource() : data(new int[100]) {}
~MyResource() { delete[] data; }
// Copy constructor
MyResource(const MyResource& other) : data(new int[100]) {
std::copy(other.data, other.data + 100, data);
}
// Copy assignment operator
MyResource& operator=(const MyResource& other) {
if (&other == this) { return *this; }
std::copy(other.data, other.data + 100, data);
return *this;
}
private:
int* data;
};
In this example, MyResource is a class that manages a resource (an array of integers), so it defines the destructor, copy constructor, and copy assignment operator.
**Rule of Five**
The Rule of Five extends the Rule of Three to include two additional special member functions:
* Move constructor
* Move assignment operator
Modern C++ introduces move semantics, which allows for more efficient handling of resources by transferring ownership without necessarily copying all the data.
_Example:_
class MyResource {
public:
// Constructors and destructor
MyResource() : data(new int[100]) {}
~MyResource() { delete[] data; }
// Copy constructor
MyResource(const MyResource& other) : data(new int[100]) {
std::copy(other.data, other.data + 100, data);
}
// Copy assignment operator
MyResource& operator=(const MyResource& other) {
if (&other == this) { return *this; }
std::copy(other.data, other.data + 100, data);
return *this;
}
// Move constructor
MyResource(MyResource&& other) noexcept : data(other.data) {
other.data = nullptr;
}
// Move assignment operator
MyResource& operator=(MyResource&& other) noexcept {
if (&other == this) { return *this; }
delete[] data;
data = other.data;
other.data = nullptr;
return *this;
}
private:
int* data;
};
In this example, MyResource is a class that manages a resource (an array of integers), so it defines all five special member functions for proper resource management and move semantics.
The Rule of Zero, Three, and Five are guidelines for managing resources within C++ classes and structs. The Rule of Zero suggests letting the compiler handle resource management if your class doesn't explicitly manage any. If your class manages resources, pre-C++11 it adhered to the Rule of Three, requiring you to define a destructor, copy constructor, and copy assignment operator. Modern C++ with move semantics extends this to the Rule of Five, which adds a move constructor and move assignment operator to efficiently transfer ownership of resources.
@@ -1,59 +1,3 @@
# First Program in C++
# Running Your First Program
In this section, we'll discuss the basic structure of a C++ program, walk you through your first program (the "Hello, World!" example), and provide additional explanations of its syntax.
Hello, World!
-------------
The first program that most people learn to write in any programming language is often a simple one that displays the message "Hello, World!" on the screen. Here's the classic "Hello, World!" program in C++:
#include <iostream>
int main() {
std::cout << "Hello, World!\n";
return 0;
}
Let's break down the different components of this program:
Header Files & Preprocessor Directives
--------------------------------------
The first line of the program `#include <iostream>` is a [preprocessor directive](https://en.cppreference.com/w/cpp/preprocessor) that tells the compiler to include the header file `iostream`. Header files provide function and class declarations that we can use in our C++ programs.
#include <iostream>
`main()` Function
-----------------
In C++, the `main()` function serves as the entry point of your program. The operating system runs your program by calling this `main()` function. It should be defined only once in your program and must return an integer. The keyword `int` is the return type of this function which is an integer. Unlike C in C++ it is mandatory to have `int` as the return type for the `main` function.
int main() {
// Your code goes here.
}
Output to the Console
---------------------
To output text to the console, we use the `std::cout` object and the insertion operator `<<`. In the "Hello, World!" example, we used the following line to print "Hello, World!" to the console:
std::cout << "Hello, World!\n";
* `std`: This is the namespace where C++ standard library entities (classes and functions) reside. It stands for "standard"
* `std::cout`: The standard "character output" stream that writes to the console
* `"Hello, World!"`: The string literal to print
* `'\n'`: The "end line" manipulator that inserts a newline character and flushes the output buffer
Return Statement
----------------
Lastly, the `return 0;` statement informs the operating system that the program executed successfully. Returning any other integer value indicates that an error occurred:
return 0;
Now that you understand the basic components of a C++ program, you can write your first program, compile it, and run it to see the "Hello, World!" message displayed on the screen.
This involves configuring your system to compile and execute C++ code. You'll need a compiler (like GCC or Clang), an IDE or text editor for writing code, and potentially a build system to manage the compilation process. A basic "Hello, World!" program is typically used to verify the setup and demonstrate the fundamental syntax.
@@ -3,68 +3,65 @@
**Scope** refers to the visibility and accessibility of variables, functions, classes, and other identifiers in a C++ program. It determines the lifetime and extent of these identifiers. In C++, there are four types of scope:
* **Global scope:** Identifiers declared outside any function or class have a global scope. They can be accessed from any part of the program (unless hidden by a local identifier with the same name). The lifetime of a global identifier is the entire duration of the program.
```cpp
#include <iostream>
int globalVar; // This is a global variable
#include <iostream>
int globalVar; // This is a global variable
int main() {
std::cout << "Global variable: " << globalVar << '\n';
}
int main() {
std::cout << "Global variable: " << globalVar << '\n';
}
```
* **Local scope:** Identifiers declared within a function or a block have a local scope. They can be accessed only within the function or the block they were declared in. Their lifetime is limited to the duration of the function/block execution.
```cpp
#include <iostream>
void localExample() {
int localVar; // This is a local variable
localVar = 5;
std::cout << "Local variable: " << localVar << '\n';
}
#include <iostream>
void localExample() {
int localVar; // This is a local variable
localVar = 5;
std::cout << "Local variable: " << localVar << '\n';
}
int main() {
localExample();
// std::cout << localVar << '\n'; //error: ‘localVar’ was not declared in this scope
}
int main() {
localExample();
// std::cout << localVar << '\n'; //error: ‘localVar’ was not declared in this scope
}
```
* **Namespace scope:** A namespace is a named scope that groups related identifiers together. Identifiers declared within a namespace have the namespace scope. They can be accessed using the namespace name and the scope resolution operator `::`.
```cpp
#include <iostream>
namespace MyNamespace {
int namespaceVar = 42;
}
#include <iostream>
namespace MyNamespace {
int namespaceVar = 42;
}
int main() {
std::cout << "Namespace variable: " << MyNamespace::namespaceVar << '\n';
}
int main() {
std::cout << "Namespace variable: " << MyNamespace::namespaceVar << '\n';
}
```
* **Class scope:** Identifiers declared within a class have a class scope. They can be accessed using the class name and the scope resolution operator `::` or, for non-static members, an object of the class and the dot `.` or arrow `->` operator.
```cpp
#include <iostream>
class MyClass {
public:
static int staticMember;
int nonStaticMember;
#include <iostream>
class MyClass {
public:
static int staticMember;
int nonStaticMember;
MyClass(int value) : nonStaticMember(value) {}
};
int MyClass::staticMember = 7;
int main() {
MyClass obj(10);
std::cout << "Static member: " << MyClass::staticMember << '\n';
std::cout << "Non-static member: " << obj.nonStaticMember << '\n';
}
MyClass(int value) : nonStaticMember(value) {}
};
int MyClass::staticMember = 7;
int main() {
MyClass obj(10);
std::cout << "Static member: " << MyClass::staticMember << '\n';
std::cout << "Non-static member: " << obj.nonStaticMember << '\n';
}
```
Understanding various types of scope in C++ is essential for effective code structuring and management of resources in a codebase.
@@ -1,60 +1,3 @@
# Setting Up C++
# Setting up your Environment
Setting up C++ requires a few steps, including installing a compiler, configuring an Integrated Development Environment (IDE), and creating a new C++ project.
1\. Installing a Compiler
-------------------------
A compiler is required to convert C++ code into machine language. Some popular C++ compilers include:
* GCC (GNU Compiler Collection) for Linux and macOS, but can also be used on Windows through MinGW
* MSVC (Microsoft Visual C++) for Windows
To install a compiler, simply follow the instructions provided by the respective websites.
2\. Configuring an IDE
----------------------
An IDE is a software application that provides facilities for programming, such as code editing, debugging, and building. Some popular C++ IDEs include:
* [@article@Visual Studio](https://visualstudio.microsoft.com/vs/features/cplusplus/) (Windows, macOS)
* [@article@Eclipse](https://eclipse.org) (Windows, macOS, Linux)
* [@article@Code::Blocks](http://www.codeblocks.org) (Windows, macOS, Linux)
After downloading and installing an IDE, you might need to configure it to use the installed compiler. Check the documentation of the respective IDE for instructions on how to do this.
3\. Creating a New C++ Project
------------------------------
Once you have your IDE and compiler set up, you can create a new C++ project and start writing code. In general, follow these steps to create a new C++ project:
* Open the IDE and create a new project.
* Select the project type (C++ Application or Console Application).
* Specify the project name and location.
* Let the IDE generate the main.cpp and build files (such as Makefile or CMakeLists.txt) for you.
Example: Hello World in C++
---------------------------
Create a new file called `main.cpp` within your project and include this code:
#include <iostream>
int main() {
std::cout << "Hello, World!\n";
return 0;
}
Then, follow the IDE's instructions to build and run your program. You should see "Hello, World!" displayed in the console.
Summary
-------
Setting up C++ involves:
* Installing a compiler (e.g. GCC, MinGW, or MSVC)
* Configuring an IDE (e.g. Visual Studio, Eclipse, or Code::Blocks)
* Creating a new C++ project and writing code
By following these steps, you'll be ready to start developing C++ applications!
Setting up your environment in C++ involves configuring your computer with the necessary tools and software to write, compile, and run C++ programs. This typically includes installing a C++ compiler (like GCC or Clang), an integrated development environment (IDE) or text editor, and potentially a build system for managing larger projects. Proper setup is key to a smooth and efficient development workflow.
@@ -1,45 +1,3 @@
# SFINAE (Substitution Failure Is Not An Error)
SFINAE is a principle in C++ template metaprogramming that allows the compiler to select the appropriate function or class when a specific template specialization fails during substitution. The term "substitution failure" refers to the process where the compiler tries to substitute template arguments into a function template or class template. If the substitution causes an error, the compiler won't consider that specific specialization as a candidate and will continue searching for a valid one.
The key idea behind SFINAE is that if a substitution error occurs, it is silently ignored, and the compiler continues to explore other template specializations or overloads. This allows you to write more flexible and generic code, as it enables you to have multiple specializations for different scenarios.
Code Example
------------
Here's an example that demonstrates SFINAE in action:
#include <iostream>
#include <type_traits>
template <typename T, typename = void>
struct foo_impl {
void operator()(T t) {
std::cout << "Called when T is not arithmetic\n";
}
};
template <typename T>
struct foo_impl<T, std::enable_if_t<std::is_arithmetic<T>::value>> {
void operator()(T t) {
std::cout << "Called when T is arithmetic\n";
}
};
template <typename T>
void foo(T t) {
foo_impl<T>()(t);
}
int main() {
int a = 5;
foo(a); // output: Called when T is arithmetic
std::string s = "example";
foo(s); // output: Called when T is not arithmetic
}
In this example, we define two `foo_impl` functions are specialized based on the boolean value of `std::is_arithmetic<T>`. The first one is enabled when `T` is an arithmetic type, while the second one is enabled when `T` is not an arithmetic type. The `foo` function then calls the appropriate `foo_impl` specialization based on the result of the type trait.
When calling `foo(a)` with an integer, the first specialization is selected, and when calling `foo(s)` with a string, the second specialization is selected. If there is no valid specialization, the code would fail to compile.
SFINAE is a core principle in C++ template metaprogramming that enables the compiler to choose the most suitable function or class template specialization during compilation. It leverages the idea that when the compiler attempts to substitute template arguments into a template and the substitution results in an invalid type or expression, this failure is not immediately treated as a compilation error. Instead, the compiler silently discards that specialization and continues searching for other viable options, thus allowing for conditional compilation based on type traits and other compile-time properties.
@@ -1 +1,3 @@
# undefined
# shared_ptr
`shared_ptr` is a smart pointer in C++ that manages dynamically allocated memory. It enables multiple pointers to safely own and share the same object. When the last `shared_ptr` pointing to an object goes out of scope, the managed object is automatically deleted, preventing memory leaks. It achieves this by maintaining a reference count that tracks the number of `shared_ptr` instances pointing to the same memory location.
@@ -1 +1,3 @@
# Smart Pointers
# Smart Pointers
Smart pointers are classes that behave like regular pointers but provide automatic memory management. They help prevent memory leaks by automatically deallocating the memory they point to when they are no longer needed. This is achieved through techniques like reference counting and RAII (Resource Acquisition Is Initialization). Essentially, they encapsulate a raw pointer and ensure that the memory it points to is freed when the smart pointer goes out of scope or is reset.
@@ -1,54 +1,3 @@
# Spack
[Spack](https://spack.io/) is a flexible package manager designed to support multiple versions, configurations, platforms, and compilers. It is particularly useful in High Performance Computing (HPC) environments and for those who require fine control over their software stack. Spack is a popular choice in scientific computing due to its support for various platforms such as Linux, macOS, and many supercomputers. It is designed to automatically search for and install dependencies, making it easy to build complex software.
Key Features
------------
* **Multi-Version Support**: Spack allows for the installation of multiple versions of packages, enabling users to work with different configurations depending on their needs.
* **Compiler Support**: Spack supports multiple compilers, including GCC, Clang, Intel, PGI, and others, allowing users to choose the best toolchain for their application.
* **Platform Support**: Spack can run on Linux, macOS, and various supercomputers, and it can even target multiple architectures within a single package.
* **Dependencies**: Spack takes care of dependencies, providing automatic installation and management of required packages.
Basic Usage
-----------
* To install Spack, clone its Git repository and set up your environment:
git clone https://github.com/spack/spack.git
cd spack
. share/spack/setup-env.sh
* Install a package using Spack:
spack install <package-name>
For example, to install `hdf5`:
spack install hdf5
* Load a package in your environment:
spack load <package-name>
For example, to load `hdf5`:
spack load hdf5
* List installed packages:
spack find
* Uninstall a package:
spack uninstall <package-name>
For more advanced usage, like installing specific versions or using different compilers, consult the [Spack documentation](https://spack.readthedocs.io/).
Spack is a package manager designed for flexibility and support for multiple versions, configurations, platforms, and compilers, making it especially useful in High Performance Computing (HPC) environments. It automates the process of installing and managing dependencies, enabling users to build complex software stacks with fine-grained control over their components. Spack supports a variety of platforms, including Linux, macOS, and supercomputers, along with compilers like GCC, Clang, and Intel. Its key features include multi-version support, compiler support, platform support, and automatic dependency management.
@@ -1 +1,3 @@
# undefined
# spdlog
spdlog is a fast and cross-platform C++ logging library. It provides a simple interface for writing log messages to different destinations such as the console, files, or custom sinks. It supports various log levels, formatting options, and asynchronous logging, making it suitable for projects that require efficient and flexible logging capabilities.

Some files were not shown because too many files have changed in this diff Show More