Skip to Content
Effective Modern C++(中文版)
book

Effective Modern C++(中文版)

by Scott Meyers
April 2018
Intermediate to advanced
303 pages
4h 54m
Chinese
China Electric Power Press Ltd.
Content preview from Effective Modern C++(中文版)
型别推导
35
条款
4
:掌握查看型别推导结果的方法
采用何种工具来查看型别推导结果,取决于你在软件开发过程的哪个阶段需要该信息。
我们将研究三个可能的阶段:撰写代码阶段、编译阶段和运行时阶段。
IDE
编辑器
IDE
中的代码编辑器通常会在你将鼠标指针悬停至某个程序实体,如变量、形参、函
数等时,显示出该实体的型别。例如以下这段代码:
const int theAnswer = 42;
auto x
= theAnswer;
auto y
= &theAnswer;
IDE
编辑器很可能会显示出,
x
的型别推导结果是
int
,而
y
则是
const int*
要让这种方法奏效,代码就多多少少要处于一种可编译的状态。因为让
IDE
提供此类
信息的工作原理是让
C++
编译器(或至少也是其前端)在
IDE
内执行一轮。如果该
编译器不能在分析你的代码时得到足够的有用信息,自然也就无法显示出推导出了何
种型别。
对于像
int
这样的平凡型别,从
IDE
能得到的信息大体良好。不过你很快就会看到,
一旦较为复杂的型别现身,
IDE
显示的信息就不太有用了。
编译器诊断信息
想要让编译器显示其推导出的型别,一条有效的途径是使用该型别导致某些编译错误。
而报告错误的消息几乎肯定会提及导致该错误的型别。
例如,我们想要查看上例中的
x
y
导而得的型别。我们先声明一个类模板,但不
去定义它。像这样的代码就能够很好地完成任务:
template<typename T>
//
只声明
TD
而不定义
;
class TD;
// TD
是“型别显示类”
(Type Displayer) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

流畅的Python(第2版)

流畅的Python(第2版)

Luciano Ramalho
Effective STL

Effective STL

Scott Meyers

Publisher Resources

ISBN: 9787519817749