sizeof(name)
alignof(name)
.................
decltype(name)
name
#name
.................
size
aligment
storage class
type
value
name
lifetime
(different than typeid(name))
(optional)
(may be undefined)
Scope (Name Validity)
Declaration point
Scope end
Block scope
Function scope
Function parameter scope
Namespace scope
Class scope
Enumeration scope
Template parameter scope
void f()
{
{
int x;
int y;
}
}
(labels only)
void f(int n)
{
}
namespace ns {
void f();
namespace {
void g();
}
inline namespace sub {
void h();
}
}
namespace ns {
}
(global namespace / global scope)
int n;
void f(int n)
{
if (n > 5)
{
int n = 3;
n = 5;
}
}
#include <iostream>
void print(int n)
{
std::cout << n << std::endl;
}
Unqualified name lookup
Qualified name lookup
{
{
{
{
x = 3;
}
}
}
}
namespace ns
{
struct X{};
void f(X);
}
void f()
{
ns::X x;
f(x);
}
Argument dependent lookup (ADL)
namespace n1
{
namespace n2::n3
{
namespace n4
{
namespace
{
inline namespace n
{
void f();
}
}
}
}
}
::n1::n2::n3::n4::f();
lifetime
(addresses)
Prototype
Address operator
Dereference operator
Member access operator
Type*
&object
*pointer
pointer->member
struct type
{
int member1;
bool member2;
}
type object = {1, true};
type* obj_ptr = &object;
(*obj_ptr) = {0, false};
obj_ptr->member12 = true;
int* f()
{
int x = 5;
return &x;
}
void g()
{
int* ptr = f();
z = *ptr; // UB
}
int x = 2;
int* x_ptr = &x;
(*x_ptr) = 5;
int y = 5;
x_ptr = &y;
x_ptr = nullptr;
int x = 2;
int& x_ref = x;
x_ref = 5;
int y;
x_ref = y; // x == y cannot rebind
// no 'null reference'
void foo(int x)
{
++x;
}
int main()
{
int n = 3;
foo(3);
int m = n;
}
void foo(int& x)
{
++x;
}
int main()
{
int n = 3;
foo(3);
int m = n;
}
void foo(int x)
{
++x;
}
int main()
{
int n = 3;
foo(3);
int m = n;
// m == 3
}
int foo(int& x)
{
++x;
}
int main()
{
int n = 3;
foo(3);
int m = n;
}
void foo(int& x)
{
++x;
}
int main()
{
int n = 3;
foo(3);
int m = n;
}
void foo(int x)
{
++x;
}
int main()
{
int n = 3;
foo(3);
int m = n;
// m == 3
}
int foo(int& x)
{
++x;
}
int main()
{
int n = 3;
foo(3);
int m = n;
}
void foo(int& x)
{
++x;
}
int main()
{
int n = 3;
foo(3);
int m = n;
// m == 4
}
void foo(int x)
{
++x;
}
int main()
{
int n = 3;
foo(3);
int m = n;
// m == 3
}
int foo(int& x)
{
++x;
}
int main()
{
int n = 3;
foo(3);
int m = n;
}
void foo(int& x)
{
++x;
}
int main()
{
int n = 3;
foo(3);
int m = n;
// m == 4
}
void foo(int x)
{
++x;
}
int main()
{
int n = 3;
foo(3);
int m = n;
// m == 3
}
int foo(int& x)
{
++x;
}
int main()
{
int n = 3;
foo(3);
int m = n;
}
void foo(int a[])
{
a[0] = 1;
}
int main()
{
int a[1] = {3};
foo(a);
int m = a[0];
// m == 1
}
#include <iostream>
int countChars(char str[], int length, char c) {
int result = 0;
for (int i = 0; i < length; ++i) {
if (str[i] == c) {
++result;
}
}
return result;
}
int main() {
std::cout << countChars("Hello World", 11, 'e');
}
#include <iostream>
int countChars(char* str, int length, char c) {
int result = 0;
for (int i = 0; i < length; ++i) {
if (str[i] == c) {
++result;
}
}
return result;
}
int main() {
std::cout << countChars("Hello World", 11, 'e');
}
const int x = 4;
x = 3; // error
int*
int* const
const int*
const int* const
const pointers
int&
const int&
const references
#include <iostream>
int countChars(const char* str, int length, char c) {
int result = 0;
for (int i = 0; i < length; ++i) {
if (str[i] == c) {
++result;
}
}
return result;
}
int main() {
std::cout << countChars("Hello World", 11, 'e');
}
#include <iostream>
#include <string_view> // C++17
int countChars(const std::string_view s, char c) {
int result = 0;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == c) {
++result;
}
}
return result;
}
int main() {
std::cout << countChars("Hello World", 'e');
}
r-value references ( )
l-value references ( )
forwarding references ( )
Type&
Type&&
Type&&
int x = 3,
y = 8;
std::string s = "Hello"
std::string s2 = std::to_string(x);
int z = x + y;
Temporary lifetime extension
Function overload selection
std::string s = "Hello";
//std::string& s_ref = s + " World";
std::string&& s_rref = s + " World";
s_rref += "!\n";
void std::string f(const std::string&);
void std::string f(std::string&&);
std::string s = "Hello";
f(s);
f(s + " World!\n");
std::vector::push_back