Core Syntax

Core Syntax

Variables, types, operators, expressions, and declarations at a glance.

Core Syntax

Basic program shape

#include <iostream>

int main() {
    std::cout << "Hello, C++\n";
    return 0;
}

Common fundamental types

Variables and initialization

int a = 1;          // copy initialization
int b(2);           // direct initialization
int c{3};           // brace initialization
const int d = 4;    // immutable
constexpr int e = 5; // compile-time constant
auto name = std::string{"Ada"};

References and qualifiers

int value = 10;
int& ref = value;          // lvalue reference
const int& cref = value;   // read-only reference
int&& rref = 42;           // rvalue reference

Operators

Casts

double x = 3.9;
int i = static_cast<int>(x);
Base* base = dynamic_cast<Base*>(ptr);
const char* raw = reinterpret_cast<const char*>(buffer);

Namespaces and aliases

namespace math {
    int square(int x) { return x * x; }
}

using Index = std::size_t;
namespace fs = std::filesystem;

Best-practice reminders

auto, decltype, and deduction

std::vector<int> values{1, 2, 3};
auto it = values.begin();          // iterator type deduced
decltype(values.size()) count = values.size();

Useful modern keywords and attributes

[[nodiscard]] int parse(std::string_view text);
constinit inline int global_counter = 0;

consteval int version() {
    return 23;
}

Comparison and aggregate improvements

struct Point {
    int x{};
    int y{};
    auto operator<=>(const Point&) const = default;
};

Point p{.x = 3, .y = 4};