c++11-17 模板核心知识(九)—— 理解decltype与decltype(auto)
与模板参数推导和auto推导一样,decltype的结果大多数情况下是正常的,但是也有少部分情况是反直觉的。
decltype介绍
给定一个name或者expression,decltype会告诉你它的类型。
我们先从正常情况开始:
const int i = 0; // decltype(i) is const int
bool f(const Widget& w); // decltype(w) is const Widget&
// decltype(f) is bool(const Widget&)
struct Point {
int x, y; // decltype(Point::x) is int
}; // decltype(Point::y) is int
Widget w; // decltype(w) is Widget
if (f(w)) … // decltype(f(w)) is bool
template<typename T> // simplified version of std::vector
class vector {
public:
…
T& operator[](std::size_t index);
…
};
vector<int> v; // decltype(v) is vector<int>
…
if (v[0] == 0) … // decltype(v[0]) is int&
很直观,没有例外情况。 注意:decltype与auto不同,不会消除const和引用。
为什么需要decltype
比如我们需要声明一个函数模板,函数的返回值类型依赖函数参数的类型。在C++11中,常见的例子是返回一个container对应索引的值:
template <typename Container, typename Index> // works, but requires refinement
auto authAndAccess(Container &c, Index i) -> decltype(c[i]) {
return c[i];
}
注意:这里的auto跟类型推导没有任何关系,它只是表明了这里使用了C++11的trailing return type.
decltype(auto)
在C++11中只允许单语句的lambda表达式被推导,在C++14中之中行为被拓展到所有lambda和所有函数,包括多语句。在C++14中,上述代码我们可以简写为:
template<typename Container, typename Index> // C++14; not quite correct
auto authAndAccess(Container& c, Index i) {
return c[i]; // return type deduced from c[i]
}
注意:这里的auto就跟类型推导有关系了。 在前面讲auto推导规则的文章中提到过,auto作用在函数返回值时,使用的是模板参数推导规则,这里就会出现问题:operator []我们希望它返回引用,但是使用auto使用模板参数推导规则时,引用会被忽略,所以下面的代码会报错:
template <typename Container, typename Index>
auto authAndAccess(Container &c, Index i) {
return c[i];
}
std::vector<int> v{1,2,3,4,5};
authAndAccess(v,2) = 10; // error: expression is not assignable
但是使用auto -> decltype()则不会报错,因为这里auto不代表参数参数推导:
template <typename Container, typename Index>
auto authAndAccess(Container &c, Index i) -> decltype(c[i]) {
return c[i];
}
std::vector<int> v{1,2,3,4,5};
authAndAccess(v,2) = 10;
所以,要想让authAndAccess在使用auto的情况下返回引用,在C++14中,我们可以使用decltype(auto):
template <typename Container, typename Index>
decltype(auto) authAndAccess(Container &c, Index i) {
return c[i];
}
std::vector<int> v{1,2,3,4,5};
authAndAccess(v,2) = 10;
decltype(auto)中的auto代表返回值需要被自动推导,decltype代表使用decltype来推导返回值类型。
decltype(auto)不仅可以声明函数返回值,还可以声明变量:
Widget w;
const Widget& cw = w; // auto type deduction : myWidget1's type is Widget
decltype(auto) myWidget2 = cw; // decltype type deduction : myWidget2's type is const Widget&
注意(entity)
decltype的规则可以看官网:decltype specifier,概况下可以分为两大类:
decltype ( entity ): 如果entity是一个不被括号包围的标识符、类访问表达式,那么decltype ( entity )与entity类型一致。decltype ( expression ): 如果expression是一个表达式,计算结果为类型T,那么:- 如果expression为xvalue,那么decltype的结果是T&&.
- 如果expression为lvalue,那么decltype的结果是T&.
- 如果expression为prvalue,那么decltype的结果是T.
注意第一点中强调了entity是一个不被括号包围的标识符。因为当一个标识符被括号包围时,它就是一个左值表达式了,对应上面第二大点的第二小点。比如说int x = 0;,x是一个标识符,所以decltype(x)的结果为int。但是(x)就是一个左值表达式,decltype((x))的结果就是int&。所以下面的用法是不同的:
decltype(auto) f1() {
int x = 0;
…
return x; // decltype(x) is int, so f1 returns int
}
decltype(auto) f2() {
int x = 0;
…
return (x); // decltype((x)) is int&, so f2 returns int&
}
官网的例子能很好的概况decltype最常见的用法:
#include <iostream>
struct A { double x; };
const A* a;
decltype(a->x) y; // type of y is double (declared type)
decltype((a->x)) z = y; // type of z is const double& (lvalue expression)
template<typename T, typename U>
auto add(T t, U u) -> decltype(t + u) // return type depends on template parameters
// return type can be deduced since C++14
{
return t + u;
}
int main()
{
int i = 33;
decltype(i) j = i * 2;
std::cout << "i = " << i << ", "
<< "j = " << j << '\n';
auto f = [](int a, int b) -> int
{
return a * b;
};
decltype(f) g = f; // the type of a lambda function is unique and unnamed
i = f(2, 2);
j = g(3, 3);
std::cout << "i = " << i << ", "
<< "j = " << j << '\n';
}
(完)
朋友们可以关注下我的公众号,获得最及时的更新:

c++11-17 模板核心知识(九)—— 理解decltype与decltype(auto)的更多相关文章
- c++11-17 模板核心知识(十一)—— 编写泛型库需要的基本技术
Callables 函数对象 Function Objects 处理成员函数及额外的参数 std::invoke<>() 统一包装 泛型库的其他基本技术 Type Traits std:: ...
- c++11-17 模板核心知识(十二)—— 模板的模板参数 Template Template Parameters
概念 举例 模板的模板参数的参数匹配 Template Template Argument Matching 解决办法一 解决办法二 概念 一个模板的参数是模板类型. 举例 在c++11-17 模板核 ...
- c++11-17 模板核心知识(十四)—— 解析模板之依赖型模板名称(.template/->template/::template)
tokenization与parsing 解析模板之类型的依赖名称 Dependent Names of Templates Example One Example Two Example Three ...
- c++11-17 模板核心知识(十五)—— 解析模板之依赖型类型名称与typename Dependent Names of Types
模板名称的问题及解决 typename规则 C++20 typename 上篇文章c++11-17 模板核心知识(十四)-- 解析模板之依赖型模板名称 Dependent Names of Templ ...
- c++11-17 模板核心知识(五)—— 理解模板参数推导规则
Case 1 : ParamType是一个指针或者引用,但不是universal reference T& const T& T* Case 2 : ParamType是Univers ...
- c++11-17 模板核心知识(二)—— 类模板
类模板声明.实现与使用 Class Instantiation 使用类模板的部分成员函数 Concept 友元 方式一 方式二 类模板的全特化 类模板的偏特化 多模板参数的偏特化 默认模板参数 Typ ...
- c++11-17 模板核心知识(一)—— 函数模板
1.1 定义函数模板 1.2 使用函数模板 1.3 两阶段翻译 Two-Phase Translation 1.3.1 模板的编译和链接问题 1.4 多模板参数 1.4.1 引入额外模板参数作为返回值 ...
- c++11-17 模板核心知识(八)—— enable_if<>与SFINAE
引子 使用enable_if<>禁用模板 enable_if<>实例 使用Concepts简化enable_if<> SFINAE (Substitution Fa ...
- c++11-17 模板核心知识(三)—— 非类型模板参数 Nontype Template Parameters
类模板的非类型模板参数 函数模板的非类型模板参数 限制 使用auto推断非类型模板参数 模板参数不一定非得是类型,它们还可以是普通的数值.我们仍然使用前面文章的Stack的例子. 类模板的非类型模板参 ...
随机推荐
- JUC---12深入理解CAS
一.什么是CAS Compare and Swap, 翻译成比较并交换,是java.util.concurrent.atomic包下的类里面的CompareAndSet()方法:java.util.c ...
- NB-IoT的数据链路层和上行传输信道类型
NB-IoT的数据链路层 NB-IoT在LTE系统的基础上对数据链路层进行了大量简化,但整体上还是保持了原有的框架.数据链路层是二层协议,包含了3个子层:MAC子层.RLC子层和PDCP子层. 以网络 ...
- 浅析I/O模型-select、poll、epoll
I/O流 概念 (1)c++中将数据的输入输出称之为流(stream),在c++中,流被定义为类,成为流类(stream class),其定义的对象为流对象. (2)文件,套接字(socket),管道 ...
- linux上性能调优常用命令及简介
1.综合命令:nmon.top:topas(aix) d :磁盘相关 c:cpu相关 m:内存相关 2.磁盘 2.1 测试顺序写性能dd if=/dev/zero of=/cdr/test.data ...
- error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build Tools“
python3 是用 VC++ 14 编译的, python27 是 VC++ 9 编译的, 安装 python3 的包需要编译的也是要 VC++ 14 以上支持的. 可以下载安装这个: 链接:htt ...
- uniapp微信小程序获取当前用户手机号码(前端)
按钮触发获取用户信息 uniapp中与微信小程序官网所写会不同, <button open-type="getPhoneNumber" @getphonenumber=&qu ...
- 实用fork/join框架提升程序效率
实用fork/join框架提成程序效率 原文地址:https://www.jianshu.com/p/9ce243796d4a 业务场景 最近再做一个接口,我是一个中央的消息接受方,当我接受到消息后要 ...
- 对比JAVA、Python、C、Go运行时间,我惊呆了!!!
对比JAVA.Python.C.Go运行时间,我惊呆了!!! 周末在寝室刷完算法,想放松一下,于是做了一个实验:用现在主流的几种编程语言对0 - (10000000 - 1)求和,结果我惊呆了,话不多 ...
- Efficient Estimation of Word Representations in Vector Space 论文笔记
Mikolov T , Chen K , Corrado G , et al. Efficient Estimation of Word Representations in Vector Space ...
- httpserver ---tcp参数设置
1.SO_REUSEADDR选项: 在服务器程序中,SO_REUSEADDR socket选项通常在调用bind()之前被设置.SO_REUSEADDR可以用在以下四种情况下: (摘自<Unix ...