惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
T
The Exploit Database - CXSecurity.com
V
Vulnerabilities – Threatpost
Know Your Adversary
Know Your Adversary
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
N
News and Events Feed by Topic
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Secure Thoughts
G
GRAHAM CLULEY
Google Online Security Blog
Google Online Security Blog
Help Net Security
Help Net Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
O
OpenAI News
Google DeepMind News
Google DeepMind News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 最新话题
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Stack Overflow Blog
Stack Overflow Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
Lohrmann on Cybersecurity
H
Hacker News: Front Page
W
WeLiveSecurity
P
Privacy International News Feed
Forbes - Security
Forbes - Security
月光博客
月光博客
PCI Perspectives
PCI Perspectives
T
Tailwind CSS Blog
N
News and Events Feed by Topic
T
Threat Research - Cisco Blogs
Engineering at Meta
Engineering at Meta
F
Full Disclosure
AI
AI
Hacker News - Newest:
Hacker News - Newest: "LLM"
Schneier on Security
Schneier on Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Visual Studio Blog
The Hacker News
The Hacker News
博客园 - 叶小钗
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
博客园 - 司徒正美
T
The Blog of Author Tim Ferriss

C++

小孩马上高一,但是想学信奥赛 c++,有什么学 c++的书推荐? - V2EX 真是意想不到的操作:有好几个人一起协作向 C++库 fmtlib 加上了 C11 包装接口,确实能用 - V2EX 基于 C++20 协程编写 gRPC 客户端与服务端 似乎在 C 的领域,让一个新程序“为未来准备好”是一件很麻烦的事 - V2EX 请教各位 centos 7.9 通过 devtoolset 启用 c++14/17 时遇到的链接问题 - V2EX 求大佬指点:Windows 上 c++部署最新 Paddleocr,无法通过内存识字 - V2EX 为 c++ 提供模式匹配 - V2EX 分享一下我个人开源的 C++23 协程网络框架 - V2EX 为什么写 C++的人年龄偏大? - V2EX 大型 c++项目,在 ai 帮助下完成 Linux 平台移植,可行性多大? - V2EX 少用 auto - V2EX 再一次感觉到 C++的恶心 - V2EX 分布式存储 - V2EX [求助] Linux 有什么好的引入 c++ 第三方库的方案 - V2EX [求助]请教一个 C++多线程的性能问题 - V2EX 2026 年找 C++的开发工作,应该学习 C++的哪个版本? - V2EX 分布式系统 - V2EX 有没有什么工具可以统计 C++项目里标识符的使用情况? - V2EX 看到一些 C++ 或者 C#项目 驼峰和下划线一块用,为啥泥? - V2EX [求助] Linux 系统下动态库卸载后全局变量未重置的问题 - V2EX 交叉编译 asop android adb 最新版的问题 - V2EX [有偿] 小白, Windows UI Automation TextPattern 检测问题求助 - V2EX 小白问个 vcpkg 相关的问题 - V2EX 记录一次踩坑过程(clion + cmake + vcpkg) - V2EX 用智能指针管理 ffmpeg 中的数据结构是有必要的吗? - V2EX 定位重载的插件或者 IDE - V2EX 想系统的学习 Modern C++,麻烦大佬们推荐一些书籍 - V2EX 困扰几天的问题,这是被 gcc 优化了吗? - V2EX 好的 c++代码是什么样的 - V2EX 为什么 C/C++ 语言的标准库不做成 Java 那样可安装的运行时? - V2EX CLion 提供非商业免费使用了 - V2EX MFC c++对话框项目使用 httplib 创建静态资源服务, Debug 时每次访问都会被断下是什么原因 - V2EX c++调试遇到奇怪问题 - V2EX 求推荐 C++ 高性能的 web 框架, curd 用 - V2EX 一个项目下有多个 cpp 程序的源文件以及多个程序共用的源文件与头文件, vscode 的 clangd 插件与 compile_commands.json 该如何配置 - V2EX C++库脚手架项目及思考 - V2EX 有人遇到过 vs2022 c++代码高亮不生效的问题吗? - V2EX 使用 C++20 协程与 ASIO 库写了个 Socks5 Server 的跨平台 Demo 程序,几乎全功能,单文件源码少于一千行 - V2EX 小众论坛问个好奇的问题关于 c++ - V2EX 市面上大部分 c++程序员都在 c++98 水平吧? - V2EX cpp 一个隐式转换奇怪的现象 - V2EX C++入门书籍,我是不是被坑了? - V2EX mac clion 调试怎么才能看到 stl 容器的值 - V2EX 有没有合适开源的 C++项目可以快速实现一些功能 - V2EX 用 c++实现 bluez 对于 31 个字节蓝牙广播限制的问题 - V2EX 寻找一个在线运行 c++的方案 - V2EX 这种情况如何消除几百个 if/else - V2EX cmake + googletest 的目录结构最佳实践是? - V2EX 为什么打印模板元编程计算阶乘结果,比打印 for 循环计算阶乘结果更耗时 - V2EX Asio 异步模型 - V2EX 求教, mac 下 cmake c++工程编译后 elf 里面还有很多路径字符串 - V2EX C 怎么进阶学 C++呀
使用匿名结构体指针作为常量来杜绝魔数,是否合理/值得? - V2EX
liuidetmks · 2025-10-11 · via C++

在项目中,用匿名结构体的地址作为常量来标识不同业务类型(例如 A 、B 、C 、D……),

这样做的目的是彻底避免魔术数字( magic number ),并且希望在逻辑判断中直接通过指针比较。

代码 h 文件

typedef struct __BizType *BizTypeRef;

// 对外暴露的常量声明
extern const BizTypeRef kBizTypeA;
extern const BizTypeRef kBizTypeB;
extern const BizTypeRef kBizTypeC;
extern const BizTypeRef kBizTypeD;

实现文件


// 定义结构体
struct __BizType {
    int value;
};

// 定义常量指针
const BizTypeRef kBizTypeA = &(const struct __BizType){ .value = 1 };
const BizTypeRef kBizTypeB = &(const struct __BizType){ .value = 2 };
const BizTypeRef kBizTypeC = &(const struct __BizType){ .value = 3 };
const BizTypeRef kBizTypeD = &(const struct __BizType){ .value = 4 };

使用

// 使用示例
void handleBiz(BizTypeRef type) {
    if (type == kBizTypeA) {
        // 处理业务 A
    } else if (type == kBizTypeB) {
        // 处理业务 B
    }
}