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

推荐订阅源

S
Secure Thoughts
罗磊的独立博客
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Last Week in AI
Last Week in AI
美团技术团队
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
月光博客
月光博客
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
W
WeLiveSecurity
H
Heimdal Security Blog
Vercel News
Vercel News
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
A
About on SuperTechFans
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
AI
AI
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
博客园_首页
The Last Watchdog
The Last Watchdog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
I
Intezer
K
Kaspersky official blog
M
MIT News - Artificial intelligence
J
Java Code Geeks
G
GRAHAM CLULEY
P
Palo Alto Networks Blog

宝硕博客

LLM 工程化在福 uu 中的落地实践 —— 假期调课的智能解析 - 宝硕博客 实现一个 CSS 词法分析器(Lexer) - 宝硕博客 向着璀璨的未来进发 —— 我的 2024 年度总结 - 宝硕博客 愿此去前路皆坦途 —— 我的 2023 年度总结 - 宝硕博客 如何创建一个打印友好型的网页 - 宝硕博客 我的 OI 生涯 —— 一名退役竞赛生的回忆录 - 宝硕博客 再见,2022 —— 我的 2022 年度总结 - 宝硕博客 我的新冠阳性日记 - 宝硕博客 USTC Hackergame 2022 Write Up - 宝硕博客 OIerDb NG —— 新一代的 OIerDb - 宝硕博客 拥抱 Atomic CSS-in-JS - 宝硕博客 使用 GitHub Actions 自动申请与部署 SSL 证书 - 宝硕博客 强制卸载三星应用分身中的残留应用 - 宝硕博客 2022 年常中集训游记 - 宝硕博客
向 #define int long long 说不 - 宝硕博客
宝硕 · 2023-02-08 · via 宝硕博客

TL;DR

#define int long long 是一种未定义行为,尽量不要在代码中使用它。

前言

在算法竞赛社区中,经常能看见有人在代码中使用 #define int long long 来偷懒。我是一直极力反对这种做法的,因为这种做法会导致代码的可读性大大降低,并带来一些难以预料的问题。

C++ 标准

在 ISO/IEC 14882:2014(E) 的 17.6.4.3.1 Macro names 一节中,有这样一段描述:

翻译并整理一下,就是:

翻译单元不可 #define#undef 词法上等同于下列部分的名称:

  • C++ 中的关键字(表 4、表 5,在 2.12 节 Keywords [lex.key] 中给出);

  • 有特殊含义的标识符(表 3,在 2.11 节 Identifiers [lex.name] 中给出);

  • 任何标准属性记号(attribute-token,在 7.6 节 Attributes [dcl.attr] 中给出)。

也就是说,标准中 并不允许 #define int 这种操作。

编译器实现

GCC

在 GCC 的 C Preprocessor 文档中 给出了下面的说明:

You may define any valid identifier as a macro, even if it is a C keyword.

也就是说,GCC 并没有严格按照标准来实现预处理器,而是稍微放宽了一些限制以允许通过这种方式来使得代码更加灵活,便于增强代码的向下兼容性。

Clang

相关文档中并未提及是否允许 define 关键字,但源代码中未见相关限制。

MSVC

#define 指令 相关文档中并未提及。

后记

使用适当的数据类型来存储数据,有利于代码的可读性和稳定性,便于编写和调试。同时,正确设置变量类型也能提高程序的运行速度和效率。因此,我们应该做好正确的数据类型定义,而不是在编写代码时滥用 #define int long long