C++ features by examples
Collaboration diagram for Language:

Classes

struct  Base11
 
struct  Derived11
 

Functions

void types_11 ()
 
void unique_pounter ()
 unique_ptr More...
 
void shared_pointer ()
 shared_ptr More...
 
void weak_pointer ()
 weak_ptr More...
 
void dynamic_memory_11 ()
 
char func_type (const int &x)
 
char func_type (int &x)
 
char func_type (int &&x)
 
template<class T >
char func_type_template (T &&x)
 Forwarding reference More...
 
void references_11 ()
 
void init_11 ()
 
auto trailing_return_type (int a) -> int
 
void copy_elision_demo ()
 copy_elision More...
 
void func_11 ()
 
constexpr int constexpr_factorial (int n)
 https://en.cppreference.com/w/cpp/language/constexpr More...
 
template<typename T >
constexpr T adder (T v)
 https://en.cppreference.com/w/cpp/language/parameter_pack More...
 
template<typename T , typename... Args>
constexpr T adder (T first, Args... args)
 

Variables

auto auto_int = 1
 https://en.cppreference.com/w/cpp/language/auto More...
 

Detailed Description

Function Documentation

◆ adder() [1/2]

template<typename T , typename... Args>
constexpr T adder ( first,
Args...  args 
)
constexpr

Definition at line 323 of file 11.cpp.

323 {
324 return first + adder(args...);
325}
constexpr T adder(T v)
https://en.cppreference.com/w/cpp/language/parameter_pack
Definition: 11.cpp:318
Here is the call graph for this function:

◆ adder() [2/2]

template<typename T >
constexpr T adder ( v)
constexpr

https://en.cppreference.com/w/cpp/language/parameter_pack

Definition at line 318 of file 11.cpp.

318 {
319 return v;
320}
Here is the caller graph for this function:

◆ constexpr_factorial()

constexpr int constexpr_factorial ( int  n)
constexpr

https://en.cppreference.com/w/cpp/language/constexpr

Definition at line 310 of file 11.cpp.

311{
312 return n <= 1 ? 1 : (n * constexpr_factorial(n - 1));
313}
constexpr int constexpr_factorial(int n)
https://en.cppreference.com/w/cpp/language/constexpr
Definition: 11.cpp:310
Here is the call graph for this function:
Here is the caller graph for this function:

◆ copy_elision_demo()

void copy_elision_demo ( )

copy_elision

Definition at line 275 of file 11.cpp.

276{
277 struct Obj2 { Obj2* orig = this; int ballast[4]; };
278 auto&& o2 = [](){ return Obj2();}();
279 assert(&o2 == o2.orig);
280}

◆ dynamic_memory_11()

void dynamic_memory_11 ( )

new,

delete

Definition at line 126 of file 11.cpp.

127{
128 auto a = new int[3] {1, 2, 3};
129 assert(a[2] == 3);
130 delete[] a;
131
132 auto as = new string[3] {"1", "2", "3"};
133 assert(as[2] == "3");
134 delete[] as; // calls destructors for all members
135
137
139
140 weak_pointer();
141}
void shared_pointer()
shared_ptr
Definition: 11.cpp:86
void weak_pointer()
weak_ptr
Definition: 11.cpp:109
void unique_pounter()
unique_ptr
Definition: 11.cpp:54
Here is the call graph for this function:
Here is the caller graph for this function:

◆ func_11()

void func_11 ( )

Definition at line 282 of file 11.cpp.

283{
284 class functor {
285 int y = 1;
286 public:
287 int operator()(int a) const {
288 return a + y;
289 }
290 };
291 functor ft;
292 assert(ft(1) == 2);
293
294 // https://en.cppreference.com/w/cpp/utility/functional/function
295 function<int(int)> ft2 = ft;
296 assert(ft(2) == 3);
297 return;
298
299 // https://en.cppreference.com/w/cpp/utility/functional/bind
300 auto binded = bind(ft2, 3);
301 assert(binded() == 5);
302
304}
void copy_elision_demo()
copy_elision
Definition: 11.cpp:275
Here is the caller graph for this function:

◆ func_type() [1/3]

char func_type ( const int &  x)

func_type - overloaded functions remove_reference

Definition at line 146 of file 11.cpp.

147{
148 assert(is_const<typename remove_reference<decltype(x)>::type>::value);
149 assert(is_lvalue_reference<decltype(x)>::value);
150 return 'C';
151}
Here is the caller graph for this function:

◆ func_type() [2/3]

char func_type ( int &&  x)

Definition at line 159 of file 11.cpp.

160{
161 assert(is_rvalue_reference<decltype(x)>::value);
162 return 'R';
163}

◆ func_type() [3/3]

char func_type ( int &  x)

Definition at line 153 of file 11.cpp.

154{
155 assert(is_lvalue_reference<decltype(x)>::value);
156 return 'L';
157}

◆ func_type_template()

template<class T >
char func_type_template ( T &&  x)

Forwarding reference

Definition at line 168 of file 11.cpp.

169{
170 // x is not R-value here
171 assert(func_type(x) != 'R');
172
173 // x can be forwarded as R or L value
174 // https://en.cppreference.com/w/cpp/utility/forward
175 return func_type(forward<T>(x)); // like func_type((T)(x));
176}
char func_type(const int &x)
Definition: 11.cpp:146
Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_11()

void init_11 ( )

direct_initialization

zero_initialization

list_initialization

initializer_list

Member_initialization

aggregate_initialization

reference_initialization

Definition at line 231 of file 11.cpp.

232{
233 struct C { int a, b, c; };
234 auto o2 = C {1, 2, 3};
235 C o3 {1, 2, 3};
236 (void) o3;
237
238 auto uses_il = [](initializer_list<int> il) {
239 assert(*il.begin() == 3);
240 assert(il.size() == 4);
241 };
242 uses_il({3, 2, 1, 0});
243
244 auto z1 = C();
245 C z2 = {};
246 auto z3 = C {};
247
248 assert(!z1.a);
249 assert(!z2.a);
250 assert(!z3.a);
251 array<int, 3> a {1, 2};
252}
Here is the caller graph for this function:

◆ references_11()

void references_11 ( )

References

is_reference, ref, reference_wrapper

Definition at line 186 of file 11.cpp.

187{
188 // https://en.cppreference.com/w/cpp/language/reference
189 assert(is_reference<int&>::value);
190
191 // L-value:
192 assert(is_lvalue_reference<int&>::value);
193
194 // R-value
195 assert(is_rvalue_reference<int&&>::value);
196
197 const int c = 1;
198 int i = 2;
199
200 assert(func_type(3) == 'R');
201 assert(func_type(c) == 'C');
202 assert(func_type(move(i)) == 'R');
203
204 assert(func_type_template(c) == 'C');
205 assert(func_type_template(i) == 'L');
206 assert(func_type_template(3) == 'R');
207 reference_wrapper<int> rw = i;
208 rw.get() = 3;
209 assert(i == 3);
210 auto cr = cref(i);
211 assert(cr == 3);
212}
char func_type_template(T &&x)
Forwarding reference
Definition: 11.cpp:168
Here is the call graph for this function:
Here is the caller graph for this function:

◆ shared_pointer()

void shared_pointer ( )

shared_ptr

Definition at line 86 of file 11.cpp.

87{
88 shared_ptr<int> s1;
89 assert(!s1);
90 assert(!s1.use_count());
91
92 auto s2 = make_shared<int>(1);
93 assert(s2.use_count() == 1);
94
95 s1 = s2;
96 assert(s1.use_count() == 2);
97
98 *s1 = 2;
99 assert(*s1 == *s1.get());
100 assert(*s2 == 2);
101
102 s2 = nullptr; // like s2.reset();
103 assert(s1.use_count() == 1);
104 assert(!s2.use_count());
105}
Here is the caller graph for this function:

◆ trailing_return_type()

auto trailing_return_type ( int  a) -> int

Definition at line 268 of file 11.cpp.

269{
270 return a;
271}
Here is the caller graph for this function:

◆ types_11()

void types_11 ( )

Definition at line 32 of file 11.cpp.

33{
34 static_assert(__cpp_decltype, "");
35
36 int a;
37 // https://en.cppreference.com/w/cpp/header/type_traits
38 decltype(a) b; // https://en.cppreference.com/w/cpp/language/decltype
39
40 assert((is_same<decltype(a), decltype(b)>::value));
41 assert((!is_same<decltype(a), unsigned>::value));
42 assert((is_same<int, int32_t>::value));
43 assert((is_same<signed, int32_t>::value));
44
45 assert(is_integral<int>::value);
46 assert(is_integral<bool>::value);
47 assert(!is_integral<float>::value);
48 assert(is_pointer<int*>::value);
49 assert(sizeof (long long) >= 8);
50}
Here is the caller graph for this function:

◆ unique_pounter()

void unique_pounter ( )

unique_ptr

Definition at line 54 of file 11.cpp.

55{
56 int d = 0;
57 unique_ptr<int> u1;
58 assert(!u1);
59 u1.reset(&d);
60 assert(u1);
61 *u1 = 1;
62 assert(d == 1);
63
64 unique_ptr<int> u2;
65 // u2 = u1; - prohibited
66
67 int * p1 = u1.get();
68 u2 = move(u1); // [move](https://en.cppreference.com/w/cpp/utility/move)
69 assert(u2.get() == p1);
70 assert(u2);
71 assert(!u1);
72 assert(u2.get() == &d);
73 // must release because d is local
74 u2.release();
75 u2.reset(new int(10));
76 assert(*u2 == 10);
77
78 u2.reset(); // deletes int(10)
79 assert(u2 == nullptr); // [nullptr](https://en.cppreference.com/w/cpp/language/nullptr)
80
81 assert(!u2);
82}
Here is the caller graph for this function:

◆ weak_pointer()

void weak_pointer ( )

weak_ptr

Definition at line 109 of file 11.cpp.

110{
111 std::weak_ptr<int> wp;
112
113 assert(!wp.lock());
114 assert(!wp.use_count());
115 auto sp = std::make_shared<int>(1);
116 wp = sp;
117 assert(*wp.lock() == 1);
118}
Here is the caller graph for this function:

Variable Documentation

◆ auto_int