C++ · 1 · Foundations4 / 36 · 11%
auto, Range-for & nullptr
Modern syntax that removes noise without hiding intent.
Examples: auto, for (x : v), nullptr, structured bindings
shortcuts: ← prev · → next · M mark
1
auto
The compiler deduces the type — the type is still static and fixed.
Example
example
auto i = 42; // int
auto d = 3.14; // double
auto& r = vec[0]; // reference, no copy
const auto& cr = big_object; // read-only, no copy
auto it = m.find(key); // spares you std::map<K,V>::iteratorWATCH OUT
Plain `auto x = expr;` always copies and drops references and const. Write auto& or const auto& when you mean a reference.
2
Range-based for
Iterate a container without an index or an iterator.
Example
example
std::vector<std::string> names{"ada", "linus"};
for (const auto& n : names) std::cout << n << '\n'; // read
for (auto& n : names) n += "!"; // modify3
nullptr
A real null-pointer type — NULL and 0 are integers in disguise.
Example
example
void f(int);
void f(char*);
f(NULL); // ambiguous / calls f(int) — surprise
f(nullptr); // unambiguously calls f(char*)4
Structured Bindings (C++17)
Destructure pairs, tuples and structs in one line.
Example
example
std::map<std::string,int> ages{{"ada",36}};
for (const auto& [name, age] : ages)
std::cout << name << " is " << age << '\n';
auto [ok, value] = try_parse(text);