| pvk_test |
| 1 | #include <iostream> #include <stdexcept> #include <vector> #include <string> #include <algorithm> #include <functional> struct A { int n; A(int n = 0): n(n) { std::cout << "A(" << n << ") constructed successfully "; } ~A() { std::cout << "A(" << n << ") destroyed "; } }; int foo() { throw std::runtime_error("error"); } struct B { A a1, a2, a3; B() try : a1(1), a2(foo()), a3(3) { std::cout << "B constructed successfully "; } catch(...) { std::cout << "B::B() exiting with exception "; } ~B() { std::cout << "B destroyed "; } }; struct C : A, B { C() try { std::cout << "C::C() completed successfully "; } catch(...) { std::cout << "C::C() exiting with exception "; } ~C() { std::cout << "C destroyed "; } }; int main () try { C c; } catch (const std::exception& e) { std::cout << "main() failed to create C with: " << e.what(); } |
| 2 | template <typename T> auto get_value(T t) { if constexpr (std::is_pointer_v<T>) return *t; else return t; } |
| 3 | template<typename T, typename ... Rest> void g(T&& p, Rest&& ...rs) { if constexpr (sizeof...(rs) > 0) g(rs...); } |
| 4 | void f() { if constexpr(false) { int i = 0; int *p = i; // Error even though in discarded statement } } |
| 5 | template<class T> void g() { auto lm = [](auto p) { if constexpr (sizeof(T) == 1 && sizeof p == 1) { g<T> } }; } |
| 6 | template<class T> struct dependent_false : std::false_type {}; template <typename T> void f() { if constexpr (std::is_arithmetic_v<T>) // ... else static_assert(dependent_false<T>::value, "Must be arithmetic"); // ok } |
| 7 | for (int i = 0; i < 10; ++i) std::cout << i << ' '; std::cout << ' '; |
| 8 | for (int i = 0, *p = &i; i < 9; i += 2) { std::cout << i << ':' << *p << ' '; } std::cout << ' '; |
| 9 | char cstr[] = "Hello"; for (int n = 0; char c = cstr[n]; ++n) std::cout << c; std::cout << ' '; |
| 10 | std::vector<int> v = {3, 1, 4, 1, 5, 9}; for (auto iter = v.begin(); iter != v.end(); ++iter) { std::cout << *iter << ' '; } std::cout << ' '; |
| 11 | int n = 0; for (std::cout << "Loop start "; std::cout << "Loop test "; std::cout << "Iteration " << ++n << ' ') if(n > 1) break; std::cout << ' '; |
| 12 | try { std::cout << "Throwing an integer exception... "; throw 42; } catch (int i) { std::cout << " the integer exception was caught, with value: " << i << ' '; } |
| 13 | int x = 4; auto y = [&r = x, x = x + 1]()->int { r += 2; return x * x; }(); auto make_function(int& x) { return [&]{ std::cout << x << ' '; }; } |
| 14 | std::vector<int> c = {1, 2, 3, 4, 5, 6, 7}; int x = 5; c.erase(std::remove_if(c.begin(), c.end(), [x](int n) { return n < x; }), c.end()); |
| 15 | std::cout << "c: "; std::for_each(c.begin(), c.end(), [](int i){ std::cout << i << ' '; }); std::cout << ' '; |
| 16 | std::cout << "before:"; std::for_each(nums.begin(), nums.end(), print); std::cout << ' '; |
| 17 | std::cout << "after: "; std::for_each(nums.begin(), nums.end(), print); std::cout << ' '; std::cout << "sum: " << s.sum << ' '; |
Комментарии