码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【C++】60 alignas 和[nodiscard]


    文章目录

      • 1、alignas
        • 语法
      • 2、alignof
        • 语法
      • 3、[[nodiscard]]
        • 语法

    1、alignas

    参考:cppreference,博客

    语法

    • alignas( expression )
    • alignas( type-id )
    • alignas( pack … )

    用于放在函数前面声明对齐方式的。

    // every object of type struct_float will be aligned to alignof(float) boundary
    // (usually 4):
    struct alignas(float) struct_float
    {
        // your definition here
    };
     
    // every object of type sse_t will be aligned to 32-byte boundary:
    struct alignas(32) sse_t
    {
        float sse_data[4];
    };
     
    // the array "cacheline" will be aligned to 64-byte boundary:
    alignas(64) char cacheline[64];
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2、alignof

    返回一个类或者结构体的对其方式

    语法

    • alignof( type-id )
    #include 
     
    struct Foo
    {
        int   i;
        float f;
        char  c;
    };
     
    // Note: `alignas(alignof(long double))` below can be simplified to simply 
    // `alignas(long double)` if desired.
    struct alignas(alignof(long double)) Foo2
    {
        // put your definition here
    }; 
     
    struct Empty {};
     
    struct alignas(64) Empty64 {};
     
    int main()
    {
        std::cout << "Alignment of"  "\n"
            "- char            : " << alignof(char)    << "\n"
            "- pointer         : " << alignof(int*)    << "\n"
            "- class Foo       : " << alignof(Foo)     << "\n"
            "- class Foo2      : " << alignof(Foo2)    << "\n"
            "- empty class     : " << alignof(Empty)   << "\n"
            "- empty class\n"
            "  with alignas(64): " << alignof(Empty64) << "\n";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    3、[[nodiscard]]

      nodiscard 是 C++17 引入的标记符。

    语法

    [[nodiscard]]或者[[nodiscard("string")]]。
    表示”不应该舍弃“,如果在实际使用的时候,舍弃了返回值,则会弹出警告。

    1. 一个被nodiscard声明的函数被调用时,比如说:
    [[nodiscard]] int func(){return 1;}; // C++17
    [[nodiscard("nodiscard_func_1")]] int func_1(){return 2;};  // C++20
    
    func(); // warning
    func_1(); // warning
    12345
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    编译器警告如下:

    warning C4834: 放弃具有 "nodiscard" 属性的函数的返回值
    warning C4858: 正在放弃返回值: nodiscard_func_1
    
    • 1
    • 2
  • 相关阅读:
    基于yolov5的草莓成熟度检测系统,可进行图像目标检测,也可进行视屏和摄像检测(pytorch框架)【python源码+UI界面+功能源码详解】
    我的数学学习回忆录——一个数学爱好者的反思(二)
    中央空调系统运行原理以及相关设备介绍
    一文弄懂 if __name__ == “__main__“:(洒洒水啦!)
    Spring循环依赖
    linux centos 挂载磁盘
    【计算机组成 课程笔记】7.3 高速缓存 Cache
    Nginx优化方案
    png图片打包plist工具,手把手教你使用pngPackerGUI_V2.0
    Unity 一些常用特性收集
  • 原文地址:https://blog.csdn.net/qq_40891541/article/details/127860585
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号