[Effective Modern C++] Item 4. Know how to view deduced types - 知道如何看待推断出的类型
条款四 知道如何看待推断出的类型
基础知识
有三种方式可以知道类型推断的结果:
- IDE编辑器
- 编译器诊断
- 运行时输出
使用typeid()以及std::type_info::name可以获取变量的类型信息,但是存在一些问题,代码如下:
template<typename T>
void f(const T& param) {
using std::cout;
cout << "T = " << typeid(T).name() << "\n";
cout << "param = " << typeid(param).name() << "\n";
}
std::vector<Widget> createVec();
const auto vw = createVec();
if(!vw.empty()) {
f(&vw[]);
} // T -> class Widget const*, param -> class Widget const*
std::type_info::name要求类型以值的方式被传递。
正确结果T为Widget const*,param为Widget const* const&,分析如下:
??vw的类型为const vector<Widget>,则&v[0]被推断为const Widget*类型,当传入f中时,传入的是指针的引用,即const Widget const*,所以T的...
可以使用boost的type_id_with_cvr获得正确的类型。
总结
- 推断类型总可以通过IDE编辑器,编译器错误信息和Boost TypeIndex库获知
- 一些工具的结果可能既不有用又不准确,所以了解C++类型的推断的规则还是最本质的
[Effective Modern C++] Item 4. Know how to view deduced types - 知道如何看待推断出的类型的更多相关文章
- [Effective Modern C++] Item 7. Distinguish between () and {} when creating objects - 辨别使用()与{}创建对象的差别
条款7 辨别使用()与{}创建对象的差别 基础知识 目前已知有如下的初始化方式: ); ; }; }; // the same as above 在以“=”初始化的过程中没有调用赋值运算,如下例所示: ...
- [Effective Modern C++] Item 6. Use the explicitly typed initializer idiom when auto deduces undesired types - 当推断意外类型时使用显式的类型初始化语句
条款6 当推断意外类型时使用显式的类型初始化语句 基础知识 当使用std::vector<bool>的时候,类型推断会出现问题: std::vector<bool> featu ...
- [Effective Modern C++] Item 5. Prefer auto to explicit type declarations - 相对显式类型声明,更倾向使用auto
条款5 相对显式类型声明,更倾向使用auto 基础知识 auto能大大方便变量的定义,可以表示仅由编译器知道的类型. template<typename It> void dwim(It ...
- [Effective Modern C++] Item 3. Understand decltype - 了解decltype
条款三 了解decltype 基础知识 提供一个变量或者表达式,decltype会返回其类型,但是返回的内容会使人感到奇怪. 以下是一些简单的推断类型: ; // decltype(i) -> ...
- [Effective Modern C++] Item 2. Understand auto type deduction - 了解auto类型推断
条款二 了解auto类型推断 基础知识 除了一处例外,auto的类型推断与template一样.存在一个直接的从template类型推断到auto类型推断的映射 三类情况下的推断如下所示: // ca ...
- [Effective Modern C++] Item 1. Understand template type deduction - 了解模板类型推断
条款一 了解模板类型推断 基本情况 首先定义函数模板和函数调用的形式如下,在编译期间,编译器推断T和ParamType的类型,两者基本不相同,因为ParamType常常包含const.引用等修饰符 t ...
- Effective Modern C++ Item 27:重载universal references
假设有一个接收universal references的模板函数foo,定义如下: template<typename T> void foo(T&& t) { cout ...
- Effective Modern C++ Item 37:确保std::thread在销毁时是unjoinable的
下面这段代码,如果调用func,按照C++的标准,程序会被终止(std::terminate) void func() { std::thread t([] { std::chrono::micros ...
- Effective Modern C++ 42 Specific Ways to Improve Your Use of C++11 and C++14
Item 1: Understand template type deduction. Item 2: Understand auto type deduction. Item 3: Understa ...
随机推荐
- ORA-00937:不是单组分组函数_Oracle
Demo: SELECT USER_ID, USER_NAME, USER_SEX, MAX(USER_AGE), SUM(USER_MONEY) AS USER_MONEY USER_TEL, US ...
- C# List<T>的详细用法
所属命名空间:System.Collections.Generic List<T>类是 ArrayList 类的泛型等效类.该类使用大小可按需动态增加的数组实现 IList<T> ...
- POJ1275/ZOJ1420/HDU1529 Cashier Employment (差分约束)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 题意:一商店二十四小时营业,但每个时间段需求的出纳员不同,现有n个人申请这份工作, ...
- Servlet开发(二)
一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些 ...
- Ecstore 2.0 报表显示空白
INSERT INTO `sdb_ectools_analysis` (`id`, `service`, `interval`, `modify`) VALUES (, ), (, ), (, ); ...
- Python变量和数据类型
十六进制用0x前缀和0-9 a-f表示 字符串是以''或""括起来的任意文本 一个布尔值只有True和False两种值 布尔值可以用and or not运算 空值是 ...
- information_schema.character_sets 学习
information_schema.character_sets 表用于查看字符集的详细信息 1.character_sets 常用列说明: 1.character_set_name: 字符集名 2 ...
- CSS3匹配屏幕横竖状态
@media是css3中新定义的,功能非常强大,下面简单讲解一下用css3的@media orientation匹配手机屏幕是横屏还是竖屏. 顾名思义PC是无法匹配横竖屏的,所以orientation ...
- Mysql基础教程——mysql之一
教程列表:http://www.dxzy163.com/view/index7627.html 41 存储过程 40 全文索引与停止词 39 索引的管 38 索引概念 37 数据库备份与恢复 36 事 ...
- Planner – 项目管理软件
http://www.appinn.com/planner/ Planner 是一款开源.易用.跨平台的项目管理软件.@appinn 二猪用了 OpenProject 几年,现在已经受够了它的各种 ...