C++20: An (Almost) Complete Overview
Expressions Pack Expansion in Lambda Captures constexpr Changes virtual functions union, try/catch, dynamic_cast, typeid allocations constexpr string & vector Concurrency Changes Atomic now: use dynamic_cast() and typeid do dynamic memory allocations, new / delete contain try/catch blocks But still cannot throw exceptions33 constexpr string & vector std::string and std::vector DoWorkPreCpp20() { std::thread job { [] { /* ... */ } }; try { // ... Do something else ... } catch (...) { job.join(); throw; // rethrow } job.join(); } void DoWork() { std::jthread0 码力 | 85 页 | 512.18 KB | 5 月前3C++20's
2016}; auto lt = ld + 1h + 30min; auto zt = zoned_time{"America/ New_York", lt}; } catch (const ambiguous_local_time& e) { std::cout << e.what() << '\n'; } } Output: 2016-11-06 local_days{Sunday[2]/March/ 2016} + 2h + 30min; auto zt = zoned_time{"America/New_York", lt}; } catch (const nonexistent_local_time& e) { std::cout << e.what() << '\n'; } } Output:0 码力 | 55 页 | 8.67 MB | 5 月前3C++23: An Overview of Almost All New and Updated Features
std::optional std::optionalParse(const std::string& s) { try { return std::stoi(s); } catch (...) { return {}; } } int main() { while (true) { std::string s; std::getline(std::cin throw MyException{"Some exception..."}; } void foo() { bar(); } int main() { try { foo(); } catch (const MyException& e) { std::println("Exception caught: {}", e.what()); std::println("Stacktrace:\n{}" 0 码力 | 105 页 | 759.96 KB | 5 月前3C++高性能并行编程与优化 - 课件 - 15 C++ 系列课:字符与字符串
的第二参数: &pos stoi 的 &pos 参数实战案例 stoi 抛出异常的情况 • 如果字符串的开头不是数字,则 stoi 会抛出 std::invalid_argument 异常,可以用 catch 捕 获。 • 但是开头可以有空格,例如 stoi(“ 42yuan”) 可以正常得到 42 ,但 stoi(“my42yuan”) 就会 出错。 • 开头也可以是正负号(‘ +’ 或 ‘ -’ size() 时,会抛出 std::out_of_range 异常终止程序。使用 gdb 等调试 器就可以在出这个异常的时候暂停,帮你调试错误 ( BV1kP4y1K7Eo )。也可以从外部函数 catch 住这个异常(以 后再讲)。 • 而 [] 则不会抛出异常,他只是简单地给字符串的首地址指针和 i 做个加法运算,得到新的指针并解引用。如果你给的 i 超过了字符 串大小 i ≥ s.size()0 码力 | 162 页 | 40.20 MB | 1 年前3Working with Asynchrony Generically: A Tour of C++ Executors
*ch); } else { printf("Interrupt!\n"); break; } } } Use done_as_optional() to “catch” the cancellation and map it into an empty optional. (demo 2)100 A RANGE OF KEYCLICK SENDERS unifex::task0 码力 | 121 页 | 7.73 MB | 5 月前3C++高性能并行编程与优化 - 课件 - 13 C++ STL 容器全解之 vector
替代 a[i] , at 函数会检测索引 i 是否越界,如果他 发现索引 i >= a.size() 则会抛出异常 std::out_of_range 让程序提前终止(或者被 try-catch 捕获),配合任意一款调试器,就可 以很快速地定位到出错点。 • 不过 at 需要额外检测下标是否越界,虽然更安 全方便调试,但和 [] 相比有一定性能损失。 • int &at(size_t0 码力 | 90 页 | 4.93 MB | 1 年前3
共 6 条
- 1