• c++新特性 noexcept 字面量 对齐方式


    noexcept 的修饰和操作

    C++11 将异常的声明简化为以下两种情况:

    1. 函数可能抛出任何异常

    2. 函数不能抛出任何异常

    void may_throw()// 可能抛出异常

    void no_throw()noexcept//不可能抛出异常

    使用 noexcept 修饰过的函数如果抛出异常,编译器会使用 std::terminate() 来立即终止程序运 行。

    noexcept 还能够做操作符,用于操作一个表达式,当表达式无异常时,返回 true,否则返回 false。
     

    1. #include
    2. void may_throw()
    3. {
    4. throw true;
    5. }
    6. auto non_block_throw=[]
    7. {
    8. may_throw();
    9. };
    10. void no_throw()noexcept
    11. {
    12. return ;
    13. }
    14. auto block_throw = []() noexcept {
    15. no_throw();
    16. };
    17. int main()
    18. {
    19. std::cout << std::boolalpha
    20. << "may_throw() noexcept? " << noexcept(may_throw()) << std::endl
    21. << "no_throw() noexcept? " << noexcept(no_throw()) << std::endl
    22. << "lmay_throw() noexcept? " << noexcept(non_block_throw()) << std::endl
    23. << "lno_throw() noexcept? " << noexcept(block_throw()) << std::endl;
    24. return 0;
    25. }

    noexcept 修饰完一个函数之后能够起到封锁异常扩散的功效,如果内部产生异常,外部也不会触发。 例如:

    1. #include
    2. void may_throw()
    3. {
    4. throw true;
    5. }
    6. auto non_block_throw=[]
    7. {
    8. may_throw();
    9. };
    10. void no_throw()noexcept
    11. {
    12. return ;
    13. }
    14. auto block_throw = []() noexcept {
    15. no_throw();
    16. };
    17. int main()
    18. {
    19. try{
    20. may_throw();
    21. }catch(...)
    22. {
    23. std::cout << " 捕获异常, 来自 my_throw()" << std::endl;
    24. }
    25. try {
    26. non_block_throw();
    27. } catch (...) {
    28. std::cout << " 捕获异常, 来自 non_block_throw()" << std::endl;
    29. }
    30. try {
    31. block_throw();
    32. } catch (...) {
    33. std::cout << " 捕获异常, 来自 block_throw()" << std::endl;
    34. }
    35. }
    36. //
    37. 捕获异常, 来自 my_throw()
    38. 捕获异常, 来自 non_block_throw()

    字面量

    传统 C++ 里面要编写一个充满特殊字符的字符串其实是非常痛苦的一件事情,比如一个 包含 HTML 本体的字符串需要添加大量的转义符,例如一个 Windows 上的文件路径经常会: C:\\File\\To\\Path。

    C++11 提供了原始字符串字面量的写法,可以在一个字符串前方使用 R 来修饰这个字符串,同时, 将原始字符串使用括号包裹,例如

    1. #include
    2. #include
    3. int main() {
    4. std::string str = R"(C:\File\To\Path)";
    5. std::cout << str << std::endl;
    6. return 0;
    7. }
    8. //
    9. C:\File\To\Path

     自定义字面量

    C++11 引进了自定义字面量的能力,通过重载双引号后缀运算符实现:

    1. #include
    2. #include
    3. std::string operator"" _wow1(const char* wow1,size_t)
    4. {
    5. return std::string(wow1)+"woooooooooow, amazing";
    6. }
    7. std::string operator""_wow2(unsigned long long i)
    8. {
    9. return std::to_string(i)+"woooooooooow, amazing";
    10. }
    11. int main()
    12. {
    13. auto str = "abc"_wow1;
    14. auto num = 1_wow2;
    15. std::cout << str << std::endl;
    16. std::cout << num << std::endl;
    17. return 0;
    18. }

    自定义字面量支持四种字面量:

    1. 整型字面量:重载时必须使用 unsigned long long、const char *、模板字面量算符参数,在上 面的代码中使用的是前者;

    2. 浮点型字面量:重载时必须使用 long double、const char *、模板字面量算符;

    3. 字符串字面量:必须使用 (const char *, size_t) 形式的参数表;

    4. 字符字面量:参数只能是 char, wchar_t, char16_t, char32_t 这几种类型。

     内存对齐

    1. 每个结构体变量的地址空间为[0,sizeof(S)],而结构体中的第一个成员变量偏移量为0,即从头开始。
    2. 其他成员变量要对齐到某个数字(对齐数)的整数倍地址处,对齐数 = min{编译器默认对齐数,该成员变量大小},在windows中,编译器默认对齐数为8;
    3. 结构体大小为最大对齐数的整数倍

    C++ 11 引入了两个新的关键字 alignof 和 alignas 来支持对内存对齐进行控制。alignof 关键字 能够获得一个与平台相关的 std::size_t 类型的值,用于查询该平台的对齐方式。当然我们有时候并不 满足于此,甚至希望自定定义结构的对齐方式,同样,C++ 11 还引入了 alignas 来重新修饰某个结构 的对齐方式。我们来看两个例子:

    1. include
    2. #include
    3. #include
    4. struct Storage {
    5. char a;
    6. int b;
    7. double c;
    8. long long d;
    9. };
    10. struct alignas(std::max_align_t) AlignasStorage {
    11. char a;
    12. int b;
    13. double c;
    14. long long d;
    15. };
    16. int main() {
    17. std::cout << alignof(Storage) << std::endl;
    18. std::cout << alignof(AlignasStorage) << std::endl;
    19. std::cout<<sizeof(Storage)<
    20. std::cout<<sizeof(AlignasStorage)<
    21. return 0;
    22. }
    23. //
    24. 8
    25. 16
    26. 24
    27. 32

    重点是:32

    char 1

    int    4

    -》8

    duoble-》16

    对齐 一次16

    32=16+16

  • 相关阅读:
    面试10.13
    overflow属性详解
    阵列信号处理——研究背景与现状
    活体检测论文 Face Anti-Spoofing with Human Material Perception 阅读笔记
    开发模型>螺旋模型
    springboot配置注入增强(二)属性注入的原理
    【Pytorch】(十四)C++ 加载TorchScript 模型
    标签转格式问题之——xml_2_txt.py
    自动驾驶感知算法实战6——目标分类详解(ResNet、VGG、GoogLeNet等)
    在Web中搜索(Searching the Web, ACM/ICPC Beijing 2004, UVa1597)rust解法
  • 原文地址:https://blog.csdn.net/qq_62309585/article/details/126787954