Core Syntax
Core Syntax
Variables, types, operators, expressions, and declarations at a glance.
Core Syntax
Variables, types, operators, expressions, and declarations at a glance.
#include <iostream>
int main() {
std::cout << "Hello, C++\n";
return 0;
}
boolchar, wchar_t, char8_t, char16_t, char32_tshort, int, long, long longfloat, double, long doublevoidint 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"};
int value = 10;
int& ref = value; // lvalue reference
const int& cref = value; // read-only reference
int&& rref = 42; // rvalue reference
+ - * / %== != < <= > >=&& || !& | ^ ~ << >>= += -= *= /= %=., ->, ::condition ? a : bdouble x = 3.9;
int i = static_cast<int>(x);
Base* base = dynamic_cast<Base*>(ptr);
const char* raw = reinterpret_cast<const char*>(buffer);
namespace math {
int square(int x) { return x * x; }
}
using Index = std::size_t;
namespace fs = std::filesystem;
auto when the type is obvious from the initializer.const by default.constexpr for values known at compile time.auto, decltype, and deductionstd::vector<int> values{1, 2, 3};
auto it = values.begin(); // iterator type deduced
decltype(values.size()) count = values.size();
auto follows the initializer and usually drops top-level const.auto& keeps reference semantics.decltype(expr) asks the compiler for the exact type of an expression.[[nodiscard]] int parse(std::string_view text);
constinit inline int global_counter = 0;
consteval int version() {
return 23;
}
[[nodiscard]] catches ignored results that probably matter.constinit guarantees static initialization without forcing constness.consteval requires evaluation at compile time.struct Point {
int x{};
int y{};
auto operator<=>(const Point&) const = default;
};
Point p{.x = 3, .y = 4};
<=> can generate all comparison operators.