14static_assert(__cplusplus == 201103,
"");
18#include <bits/stdc++.h>
20#if __has_include (<version>)
34 static_assert(__cpp_decltype,
"");
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));
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);
69 assert(u2.get() == p1);
72 assert(u2.get() == &d);
75 u2.reset(
new int(10));
79 assert(u2 ==
nullptr);
90 assert(!s1.use_count());
92 auto s2 = make_shared<int>(1);
93 assert(s2.use_count() == 1);
96 assert(s1.use_count() == 2);
99 assert(*s1 == *s1.get());
103 assert(s1.use_count() == 1);
104 assert(!s2.use_count());
111 std::weak_ptr<int> wp;
114 assert(!wp.use_count());
115 auto sp = std::make_shared<int>(1);
117 assert(*wp.lock() == 1);
128 auto a =
new int[3] {1, 2, 3};
132 auto as =
new string[3] {
"1",
"2",
"3"};
133 assert(as[2] ==
"3");
148 assert(is_const<
typename remove_reference<
decltype(x)>::type>::value);
149 assert(is_lvalue_reference<
decltype(x)>::value);
155 assert(is_lvalue_reference<
decltype(x)>::value);
161 assert(is_rvalue_reference<
decltype(x)>::value);
189 assert(is_reference<int&>::value);
192 assert(is_lvalue_reference<int&>::value);
195 assert(is_rvalue_reference<int&&>::value);
207 reference_wrapper<int> rw = i;
233 struct C {
int a, b, c; };
234 auto o2 = C {1, 2, 3};
238 auto uses_il = [](initializer_list<int> il) {
239 assert(*il.begin() == 3);
240 assert(il.size() == 4);
242 uses_il({3, 2, 1, 0});
251 array<int, 3> a {1, 2};
277 struct Obj2 { Obj2* orig =
this;
int ballast[4]; };
278 auto&& o2 = [](){
return Obj2();}();
279 assert(&o2 == o2.orig);
287 int operator()(
int a)
const {
295 function<int(
int)> ft2 = ft;
300 auto binded = bind(ft2, 3);
301 assert(binded() == 5);
306static_assert(__cpp_constexpr,
"");
322template<
typename T,
typename... Args>
323T
constexpr adder(T first, Args... args) {
324 return first +
adder(args...);
327static_assert(
adder(1,2,3) == 6,
"");
352 auto annotated_named_lambda_expression =
362 auto closure = [] { };
367 auto pass = [] (
int a) {
return a; };
368 assert(pass(5) == 5);
372 auto get_i = [=] () {
return c; };
373 assert(get_i() == 1);
377 auto inc_get = [&] {
return ++c; };
378 assert(inc_get() == 2);
379 assert(inc_get() == 3);
401 assert([](
int a) {
return a + 1; }(1) == 2);
415 assert([=]{
return i; }() == 2);
418 [&](
int a){i = a;}(3);
423 [i, &j](){ j = i; }();
428 [=, &j](){ j = i; }();
433 [&, i](){ j = i; }();
437 auto inc_global = [] () {
return ++glob; };
438 assert(inc_global() == 1);
439 assert(inc_global() == 2);
447 vector<int> v = {1, 2, 3};
448 assert(v.data()[2] == 3);
452 v.emplace(v.cbegin(), 0);
453 assert(v.front() == 0);
456 assert(v.back() == 4);
458 array<int, 1> a1, a2;
462 forward_list<int> fl;
465 assert(fl.front() == 1);
467 assert(fl.front() == 2);
468 fl.insert_after(fl.cbegin(), 3);
469 assert(fl.front() + 1 == 3);
470 assert(fl.front() == 2);
471 fl.erase_after(fl.cbefore_begin());
472 assert(fl.front() == 3);
480 vector<int> v = {1, 2, 3};
481 assert(find(begin(v), end(v), 0) == end(v));
482 assert(find(begin(v), end(v), 1) != end(v));
491 array<int, 10> s {5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
492 sort(s.begin(), s.end(),
511static int use_lambda(
int a, function<
int(
int)> f)
517static function<int(
int)> g_f;
519static void set_lambda(function<
int(
int)> &&f)
524static int call_lambda(
int a)
533 auto increment = [] (
int a) ->
int {
return a + 1; };
534 assert(increment(5) == 6);
537 assert(use_lambda(2, increment) == 3);
538 set_lambda(increment);
539 assert(call_lambda(3) == 4);
542 assert(use_lambda(1, [](
int a) {
return a + 1;}) == 2);
545 [x] ()
mutable { assert(++x);}();
562 this_thread::yield();
563 this_thread::sleep_for(chrono::milliseconds(20));
576 condition_variable cv;
577 thread initiator([&m, &ready, &cv] {
578 unique_lock<mutex> lk(m);
584 unique_lock<mutex> lk(m);
585 cv.wait(lk, [&ready] {
return ready;});
591 this_thread::yield();
592 this_thread::get_id();
593 this_thread::sleep_for(chrono::nanoseconds(1));
595 future<int> f = p.get_future();
603 assert(f.get() == 2);
608 assert(!t.joinable());
609 assert(t2.joinable());
614 }
catch(
const system_error& e) {
615 assert(e.code().value() == 22);
617 assert(!t2.joinable());
634 int unguarded = 0, guarded = 0;
637 thread t1([&unguarded, &guarded, &m]{
638 unguarded = lento(unguarded) + 1;
640 lock_guard<mutex> guard(m);
641 guarded = lento(guarded) + 1;
643 thread t2([&unguarded, &guarded, &m]{
644 unguarded = lento(unguarded) + 1;
646 lock_guard<mutex> guard(m);
647 guarded = lento(guarded) + 1;
650 assert(unguarded == 0);
651 assert(guarded == 0);
654 assert(unguarded == 1);
655 assert(guarded == 2);
699 signal(SIGALRM,
sig);
static void lambda_complex(void)
static void lambda_basics(void)
void container_11()
container
void algorithm_11()
algorithm
static void lambda_capture(void)
void sort_11()
Compare with sort_03.
auto auto_int
https://en.cppreference.com/w/cpp/language/auto
char func_type(const int &x)
void shared_pointer()
shared_ptr
void copy_elision_demo()
copy_elision
constexpr T adder(T v)
https://en.cppreference.com/w/cpp/language/parameter_pack
constexpr int constexpr_factorial(int n)
https://en.cppreference.com/w/cpp/language/constexpr
auto trailing_return_type(int a) -> int
void weak_pointer()
weak_ptr
void unique_pounter()
unique_ptr
char func_type_template(T &&x)
Forwarding reference
void condition_variable_11()
void method1() override
override
void method2() final
final