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

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Project Zero
Project Zero
V
Vulnerabilities – Threatpost
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
C
Cisco Blogs
A
Arctic Wolf
月光博客
月光博客
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
量子位
小众软件
小众软件
Latest news
Latest news
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
N
Netflix TechBlog - Medium
K
Kaspersky official blog
人人都是产品经理
人人都是产品经理
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
Y
Y Combinator Blog
P
Proofpoint News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
S
Schneier on Security
D
Docker
Scott Helme
Scott Helme
MyScale Blog
MyScale Blog
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
GbyAI
GbyAI
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
H
Help Net Security
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tenable Blog
B
Blog
Know Your Adversary
Know Your Adversary
IT之家
IT之家

宝硕博客

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