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

推荐订阅源

Vercel News
Vercel News
Recorded Future
Recorded Future
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
Y
Y Combinator Blog
N
News | PayPal Newsroom
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Help Net Security
Help Net Security
博客园 - Franky
SecWiki News
SecWiki News
Recent Announcements
Recent Announcements
T
Troy Hunt's Blog
The Register - Security
The Register - Security
The Last Watchdog
The Last Watchdog
Webroot Blog
Webroot Blog
S
Security Affairs
博客园 - 司徒正美
S
Schneier on Security
I
InfoQ
博客园_首页
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Threat Research - Cisco Blogs
Forbes - Security
Forbes - Security
腾讯CDC
N
Netflix TechBlog - Medium
N
News and Events Feed by Topic
Cloudbric
Cloudbric
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta
Recent Commits to openclaw:main
Recent Commits to openclaw:main
B
Blog
V
Vulnerabilities – Threatpost
C
Check Point Blog
Google DeepMind News
Google DeepMind News
Google Online Security Blog
Google Online Security Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cisco Blogs
Schneier on Security
Schneier on Security
O
OpenAI News
K
Kaspersky official blog

Jun's Blog

2023 年度总结 OS 学习记 之 XV6 如何编译 GraalVM LLVM 中端优化之 InstCombine C++ 中 inline 关键字的语义 链接与库 C++ 移动语义基础 如何优化矩阵相乘 2022年度总结 CUDA初学笔记 汇编语言之保护模式 汇编语言之实模式 浅析 libc++ 中的 string 实现 std::expected 基本使用 C++模板基础 CSAPP第九章笔记之虚拟内存 CSAPP第八章笔记之异常控制流 2021年度总结 编译安装GCC12 如何给LLVM贡献代码 CSAPP第三章笔记Part 2 CSAPP第三章笔记Part 1 GDB基本使用笔记 给计算机新生的一封信 CMake学习笔记 由Redis学习数据结构--字典 C++中lambda表达式基础 由Redis学习数据结构--链表 对Python及爬虫行业的思考 Vim实用技巧 浅谈C++中的类 简要剖析const关键字 正则表达式基础总结 使用Hugo和Firebase部署个人博客 浅谈HTTPS证书 Linux常用命令总结 Git学习笔记 Linux下使用v2ray 娱乐至死读书笔记 自控力读书笔记 少有人走的路力读书笔记 Postfix & Dovecot 自建邮箱服务 Linux用户管理 Linux开机流程 Linux文件与目录 Linux硬盘管理 Linux服务浅谈 About Me
读Effetive Modern C++ 之类型推导
Jun · 2021-08-11 · via Jun's Blog

· Jun

概览

C++98 => 一套规则

C++11 => 新加两套,一套用于auto,一套用于decltype

C++14 => 拓展了语境

模板推导

1
2
template<typename T>
void f(ParamType param) {}

T推导的结果,不仅与实参expr有关,还与ParamType的形式有关系

ParamType 为指针或引用

推导过程中会忽略其引用性。

ParamType 为为万能引用

  • 如果expr为左值,会被推导为左值引用
  • 如果为右值,则与第一种情况相同

ParamType 为其它值

推导过程中会忽略其引用性,常量性。

只忽略顶层const, 如 const char* const 会被推导为 const char*

auto 推导

当某变量使用auto来声明时,auto就扮演了模板中的T,而变量的修饰词则扮演了ParamType

相当于:

1
2
template<typename T>
void f(const T param) {}

在大多数情况下,auto推导与模板推导没有区别,除了以下情况:

对于大括号初始化表达式的处理方式,是auto类型推导和模板类型推导的唯一不同之处。当使用auto时,推导的类型为 std::initializer_list 的一个实例。

decltype 推导

在C++11中,decltype的主要用途就在于声明返回值类型依赖与形参类别的函数模板。

在函数返回类型的推导中,auto 会忽略其引用性,我们可以使用 decltype(auto) 来修正这一问题。

如果仅有一个名字的话,其行为保持不变,若是比仅有名字更复杂的左值表达式的话,decltype保证得出的型别总为左值引用。只要不是T,它就得出T&

在C++的定义中,(x) 也是左值,所以 decltype((x)) 结果也为左值引用。