Item4 Know how to view deduced types
来源:互联网 发布:大数据都包括什么 编辑:程序博客网 时间:2024/06/04 19:02
这个系列的文章来自于Effective Modern C++的读书笔记,我抽取了其中比较重要的,不容易理解的,平常我们开发过程中也不太在意的一些Item进行分析。
在Item3中学习了C++11新特性decltype
,decltype可以获取变量或者表达式的类型,但是获取到的类型只能用于定义其他的变量和类型,不能打印出来,也不能用来操作。毕竟是编译期实现,用来做类型反射就算了,那么至少也应该可以打印输出下吧,毕竟书中得来终觉醒。那么本文就介绍几种方法来得到decltype的返回类型的名字。
IDE Editors
最简单的就是依靠C++的IDE帮你识别出decltype
的返回类型,IDE毕竟不是万能的,所以你要识别的类型要尽可能的简单,不能过于复杂。Compiler Diagnostic
借助于编译器的诊断错误信息。通过错误使用decltype
推导出来的类型让编译器报出编译错误,在编译错误的信息中可以发现decltype
推导出来的类型名称。例如下面的这个例子:
template<typename T>class TDTD<decltype(x)> xType;
使用g++编译后,会出现编译出错,诊断信息如下:
错误:聚合‘TD<int> xType’类型不完全,无法被定义 TD<decltype(x)> xType;
从上面的诊断信息就可以得出decltype(x)
的结果就是int
。
- Runtime Output
最后一种方式就比较专业了,而且还是运行时获取,不光光可以用来验证decltype
的返回类型,还可以做运行时的检查,和一些额外的操作,实现的手段则是利用了typeinfo,
#include <typeinfo>int x = 0;std::cout << typeid(decltype(x)).name() << std::endl;
上面会输出x的类型的名称,这里应该会输出int
,但也不尽然,typeid
的输出结果取决于编译器,MSVC的输出是int
,而g++
的输出则是i
,也就是c++对int的名称重写后的结果。g++其实也可以实现和MSVC的输出结果一样,
#include <string>#include <cxxabi.h>#include <memory>#include <typeinfo>#include <iostream>std::string demangle(const char* name) { int status = -4; // some arbitrary value to eliminate the compiler warning // enable c++11 by passing the flag -std=c++11 to g++ std::unique_ptr<char, void(*)(void*)> res { abi::__cxa_demangle(name, NULL, NULL, &status), std::free }; return (status==0) ? res.get() : name ;}int main() { int x; std::cout << demangle(typeid(decltype(x)).name()) << std::endl;}
到此为止typeinfo
看似解决了问题,其实不然,通过typeinfo
得到的类型会忽略cv
限制符还有引用,真的是差强人意啊。但是对const
的指针类型是不会忽略const
限制符的。具体可以参考typeid获取完整类型
幸好可以借助于boost的Boost.Type-Index
库得到精确的类型。
template<typename T>void f(const T& param) { using std::cout; using boost::typeindex::type_id_with_cvr; cout << "param = " << type_id_with_cvr<T>().pretty_name() << '\n'; cout << "param = " << type_id_with_cvr<decltype(param)>().pretty_name() << '\n';}
- Item4 Know how to view deduced types
- <Effective Mordern C++>笔记:Item 4:Knowing how to view deduced types .
- How to know whether the attribute belongs to one types of productin Magento?
- How to know firmware version
- #include---I know how to use
- how to know the type of browser
- How To Learn What Great People Know
- don’t know how to make ‘TargetCompilePass’
- How to know 2 strings are anagram
- Know How to Use Command-line Tools
- Know How to Use Command-Line Tools
- GDB: don't know how to run
- list切片(know how to slice sequences)
- How to: Convert Between Various String Types
- How to: Convert Between Various String Types
- How to: Convert Between Various String Types
- how to extend C style IO to user defined types
- how to know whether an element has an class
- runtime源码探究(一) weak的实现
- 1. 什么是Node.js
- WebChromeClient的简单使用
- Git的介绍和安装
- 如何删除GitHub上的项目仓库
- Item4 Know how to view deduced types
- jquery即点即改
- Oracle rownum与分页
- LeetCode 153 Find Minimum in Rotated Sorted Array (二分)
- 开篇导论(操作系统概念第七版 第一章)
- 微机原理--第二章(3)8086的操作和时序
- 【DISTINCT】优化之MySQL官方文档翻译
- 谨记帖——勿眼高手低
- 问题 A: Elevator