C++ features by examples
Collaboration diagram for Library:

Functions

void map_demo ()
 
void variant_demo ()
 
void clamp_demo ()
 
void dynamic_memory_17 ()
 
void string_view_demo ()
 

Detailed Description

lang17

Function Documentation

◆ clamp_demo()

void clamp_demo ( )

clamp(x, low, high) == x < low ? low : x > high ? high : x;

Definition at line 510 of file 17.cpp.

511{
512 static_assert(__cpp_lib_clamp);
514 assert(clamp(0, 1, 3) == 1);
515 assert(clamp(2, 1, 3) == 2);
516 assert(clamp(4, 1, 3) == 3);
517}
Here is the caller graph for this function:

◆ dynamic_memory_17()

void dynamic_memory_17 ( )

Definition at line 519 of file 17.cpp.

520{
521 int d[2] = {10, 11};
522 unique_ptr<int[]> u1(d);
523 assert(u1.get()[0] == 10); // C++11
524 assert(u1[1] == 11);
525 assert((bool)u1);
526 u1.release();
527 const shared_ptr<int> s1{0};
528 //cout << s1.element_type.name() << endl;
529 //cout << typeid(s1.weak_type).name() << endl;
530 assert(typeid(reinterpret_pointer_cast<shared_ptr<int>>(s1)).name()
531 == string("St10shared_ptrIS_IiEE"));
532 vector<int> s = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
533
534 // https://en.cppreference.com/w/cpp/numeric/valarray/deduction_guides
535 int a[] = {1, 2, 3};
536 valarray va(a, 3); // uses explicit deduction guide
537 static_assert(is_integral_v<remove_reference<decltype(va[0])>::type>);
538}
Here is the caller graph for this function:

◆ map_demo()

void map_demo ( )

https://en.cppreference.com/w/cpp/container/map/extract

https://en.cppreference.com/w/cpp/container/map/insert

https://en.cppreference.com/w/cpp/container/map/merge

Definition at line 452 of file 17.cpp.

453{
455 map<int, string> m{{1, "mango"}, {2, "papaya"}, {3, "guava"}};
456 auto nh = m.extract(2);
457 nh.key() = 4;
459 m.insert(move(nh));
460 // m == {{1, "mango"}, {3, "guava"}, {4, "papaya"}}
461
463 set<int> src {1, 3, 5};
464 set<int> dst {2, 4, 5};
465 dst.merge(src);
466 // src == { 5 }
467 // dst == { 1, 2, 3, 4, 5 }
468}
Here is the caller graph for this function:

◆ string_view_demo()

void string_view_demo ( )

Definition at line 541 of file 17.cpp.

542{
543 static_assert(__cpp_lib_string_view);
544 string s = "abcd";
545 string_view v = s;
546 assert(v.data() == s.c_str());
547 assert(v.substr(1, 2).data() >= s.c_str());
548 assert(v.substr(1, 2).data() <= s.c_str() + s.length());
549}
Here is the caller graph for this function:

◆ variant_demo()

void variant_demo ( )

variant

visit

Definition at line 476 of file 17.cpp.

477{
478 variant<int, float> v, w;
479 v = 12; // v contains int
480 int i = get<int>(v);
481 w = get<int>(v);
482 w = get<0>(v); // same effect as the previous line
483 w = v; // same effect as the previous line
484
485 // get<double>(v); // error: no double in [int, float]
486 // get<3>(v); // error: valid index values are 0 and 1
487
488 try {
489 get<float>(w); // w contains int, not float: will throw
490 } catch (const bad_variant_access&) {}
491
492 using namespace literals;
493
494 variant<string> x("abc");
495
496 // converting constructors work when unambiguous
497 x = "def"; // converting assignment also works when unambiguous
498
499 variant<string, void const*> y("abc");
500
501 // casts to void const * when passed a char const *
502 assert(holds_alternative<void const*>(y));
503 y = "xyz"s;
504 assert(holds_alternative<string>(y));
505
506 auto inc = [](int a) -> int { return a + 1; };
507 assert(visit(inc, (variant<int>)1) == 2);
508}
constexpr int(* inc)(int)
Definition: 17.cpp:174
Here is the caller graph for this function: