[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 ...
随机推荐
- java菜鸟篇<一> 对JsonObject 和JsonArray知识点理解
今天遇到从前台拿值(json数组格式),从request里边取值,然后经过一系列的处理方式,在用request返回去 1.先把request里的值赋值给String string类型的变量 2.因为前 ...
- uvA Flooded!
Description To enable homebuyers to estimate the cost of flood insurance, a real-estate firm provide ...
- JavaScript中的作用域链原理
执行环境 作用域链的形成与执行环境(Execution Environment)相关,在JavaScript当中,产生执行环境有如下3中情形: 1 进入全局环境 2 调用eval函数 3 调用func ...
- PHP图片加文字水印和图片水印方法
文字水印 $dst_path = 'dst.jpg'; //创建图片的实例$dst = imagecreatefromstring(file_get_contents($dst_path)); //打 ...
- Sublime text3使用技巧及快捷键
一.快速查找文件Crtl+P(Goto->Goto Anyghing) 在打开的搜索框中输入文件名按Enter键即可. 提示:1.支持文件夹+文件名的搜索,比如 "js/main.js ...
- project euler 26:Reciprocal cycles
A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with d ...
- MVC中配置OutputCache的VaryByParam参数无效的问题
在项目使用OutputCacheAttribute是遇到了问题,当我想在配置文件web.config中配置OutputCache的VaryByParam时竟然不起作用,下面是相关代码: 文件FaceC ...
- SQL Server索引语法 <第四篇>
从CREATE开始 通过显式的CREATE INDEX命令 在创建约束时作为隐含的对象 随约束创建的隐含索引 当向表中添加如下两种约束之一时,就会创建隐含索引. 主键约束(聚集索引) 唯一约束(唯一索 ...
- C# is 与 as 运算符
as运算符有一定的适用范围,它只适用于引用类型或可以为null的类型,而无法执行其他的转换,如值类型的转换以及用户自定义的类型转换,这类转换应该适用强制转换表达式来执行.as当转换不了的时候返回nul ...
- Wpf中MediaElement循环播放
原文:Wpf中MediaElement循环播放 前一段时间做了一个项目,里面牵涉到媒体文件的循环播放问题,在网上看了好多例子,都是在xaml中添加为MediaElement添加一个TimeLine,不 ...