条款三 了解decltype

基础知识

提供一个变量或者表达式,decltype会返回其类型,但是返回的内容会使人感到奇怪。

以下是一些简单的推断类型:

const int i = ; // decltype(i) -> const int
bool f(const Widget& w); // decltype(w) -> const Widget&, decltype(f) -> bool(const Widget&)
struct Point {
int x, y;
} // decltype(Point::x) -> int
Widget w; // decltype(w) -> Widget
if(f(w)); // decltype(f(w)) -> bool
template<typename T>
class vector {
public:
T& operator[](std::size_t index);
};
vector<int> v; // decltype(v) -> vector<int>
if(v[] == ); // decltype(v[0]) -> int&

在C++11中,decltype的主要作用是推断根据形参类型推断返回类型。

对于std::vector<bool>,operator[]返回的并不是bool&,而是一个新的对象。

一种返回类型的推断的用法:

template<typename Container, typename Index>
auto authAndAccess(Container& c, Index i) -> decltype(C[i]){
authenticateUser();
return c[i];
}

C++11允许允许单语句lambda返回类型的推断,C+14扩展到所有lambda和函数。

// C++14版本,但是会有问题
template<typename Container, typename Index>
auto authAndAccess(Container& c, Index i){
authenticateUser();
return c[i];
}

以上函数虽然使用了auto,但套用auto的规则会出问题。如以上c[i]返回int&,但是根据推断规则会去掉引用类型,造成不能修改。

以下代码可以正确返回类型:

// C++14版本,可以正确返回类型
template<typename Container, typename Index>
decltype(auto) authAndAccess(Container& c, Index i){
authenticateUser();
return c[i];
}

auto与decltype(auto)的区别:

Widget w;
const Widget& cw = w;
auto myWidget1 = cw; // myWidget1 -> Widget
decltype(auto) myWidget2 = cw; // myWidget2 -> const Widget&

为了使得函数可以同时传入左值和右值,函数引入通用引用,正确形式如下:

// final C++14 version
template<typename Container, typename Index>
decltype(auto) authAndAccess(Container&& c, Index i){
authenticateUser();
return std::forward<Container>(c)[i];
} // final C++1 version
template<typename Container, typename Index>
auto authAndAccess(Container&& c, Index i)
-> decltype(std::forward<Container>(c)[i]){
authenticateUser();
return std::forward<Container>(c)[i];
}

在使用decltype时,变量外加上括号会改变推断类型:

int x = ;
decltype(x); // -> int
decltype((x)); // -> int&

总结

  • decltype总是产生没经过修改的变量或表达式的类型
  • 对于类型为T的左值表达式而非变量名,decltype总是返回T&类型
  • C++14支持decltype(auto),其就像auto一样从初始化中推断类型,但是使用decltype的来推断类型

[Effective Modern C++] Item 3. Understand decltype - 了解decltype的更多相关文章

  1. [Effective Modern C++] Item 2. Understand auto type deduction - 了解auto类型推断

    条款二 了解auto类型推断 基础知识 除了一处例外,auto的类型推断与template一样.存在一个直接的从template类型推断到auto类型推断的映射 三类情况下的推断如下所示: // ca ...

  2. [Effective Modern C++] Item 1. Understand template type deduction - 了解模板类型推断

    条款一 了解模板类型推断 基本情况 首先定义函数模板和函数调用的形式如下,在编译期间,编译器推断T和ParamType的类型,两者基本不相同,因为ParamType常常包含const.引用等修饰符 t ...

  3. [Effective Modern C++] Item 7. Distinguish between () and {} when creating objects - 辨别使用()与{}创建对象的差别

    条款7 辨别使用()与{}创建对象的差别 基础知识 目前已知有如下的初始化方式: ); ; }; }; // the same as above 在以“=”初始化的过程中没有调用赋值运算,如下例所示: ...

  4. [Effective Modern C++] Item 6. Use the explicitly typed initializer idiom when auto deduces undesired types - 当推断意外类型时使用显式的类型初始化语句

    条款6 当推断意外类型时使用显式的类型初始化语句 基础知识 当使用std::vector<bool>的时候,类型推断会出现问题: std::vector<bool> featu ...

  5. [Effective Modern C++] Item 5. Prefer auto to explicit type declarations - 相对显式类型声明,更倾向使用auto

    条款5 相对显式类型声明,更倾向使用auto 基础知识 auto能大大方便变量的定义,可以表示仅由编译器知道的类型. template<typename It> void dwim(It ...

  6. [Effective Modern C++] Item 4. Know how to view deduced types - 知道如何看待推断出的类型

    条款四 知道如何看待推断出的类型 基础知识 有三种方式可以知道类型推断的结果: IDE编辑器 编译器诊断 运行时输出 使用typeid()以及std::type_info::name可以获取变量的类型 ...

  7. Effective Modern C++ Item 27:重载universal references

    假设有一个接收universal references的模板函数foo,定义如下: template<typename T> void foo(T&& t) { cout ...

  8. Effective Modern C++ Item 37:确保std::thread在销毁时是unjoinable的

    下面这段代码,如果调用func,按照C++的标准,程序会被终止(std::terminate) void func() { std::thread t([] { std::chrono::micros ...

  9. 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 ...

随机推荐

  1. UITextView(文本视图) 学习之初体验

    UITextView文本视图相比与UITextField直观的区别就是UITextView可以输入多行文字并且可以滚动显示浏览全文.常见UITextView使用在APP的软件简介.内容详情显示.小说阅 ...

  2. QT5.5实现串口通信

    QT5.1以上版本自带QtSerialPort集成库,只要在头文件中集成 #include <QtSerialPort/QSerialPort> #include <QtSerial ...

  3. UVA 10253 Series-Parallel Networks (树形dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Series-Parallel Networks Input: standard ...

  4. (原)Matlab的svmtrain和svmclassify

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5554551.html 参考网址: http://www.cnblogs.com/zhangchaoya ...

  5. VIM编辑器操作指令

    VIM有三种操作模式: 1,命令模式--command mode 2,输入模式--insert mode 3,底行模式--last line mode [在命令模式的时候,按Shift +  :出现的 ...

  6. JavaScript权威指南阅读笔记3

    第六章 对象 1.首先是先介绍了对象直接量的格式:对象直接量就是1.由若干个名/值对组成的映射表,2名/值对中间由冒号分割,3名值对之间由逗号分割,4整个映射表由花括号括起来.这样就组成了一个对象直接 ...

  7. automation studio 6.0 破解版 32位

    破解软件在iso文件的patch目录下 链接:http://pan.baidu.com/s/1o8KR7rc 密码:y87g   

  8. MVC中一般为什么用IQueryable而不是用IList?用IQueryable比IList好在哪?

    IList(IList<T>)会立即在内存里创建持久数据,这就没有实现"延期执行(deferred execution)",如果被加载的实体有关联实体(associat ...

  9. mongodb batchInsert

    看到<MongoDB 权威指南>第二版P30提到了一个batchInset的插入方法,对于一次性插入大量数据时可以提高性能.按照书上的列子,却提示错误: 本书是基于MongoDB V2.4 ...

  10. Android 中常用代码片段

    一:AsyncTask 的使用 (1)activity_main.xml <TextView android:id="@+id/tvInfo" android:layout_ ...