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的例子. 类模板的非类型模板参 ...
随机推荐
- 【问题记录】—.NetCore 编译问题
最近在协助验证Jenkins自动编译发布时,对一些.Net Core编译问题进行了解决:特记录一下 一.编译生成netcoreapp目录问题 问题现象 .net core项目编译输出目录总是包含在[n ...
- pip升级失败
python -m pip install --upgrade pip失败 解决办法: easy_install pip
- python爬虫06取当当网 Top 500 本五星好评书籍
主要思路 使用 page 变量来实现翻页 我们使用 requests 请求当当网 然后将返回的 HTML 进行正则解析 由于我们暂时还没学到数据库 所以解析完之后就把内容存到文件中 def main( ...
- php 获取抖音id
<?php public function getid($dy_url){ $header=get_headers($dy_url); $str = "/^.*?(\d+).*/&qu ...
- Interface(接口分享)第一节
一.接口初探 有时候我们传入的参数可能会包含很多的属性,但是编译器只会检查那些必须的属性是否存在,以及类型是否匹配,而咱们要讲的接口其实就是用来描述下面这个例子里的结构,对于接口传入的数据咱们只关心它 ...
- RPC协议实践入门
RPC 是什么 RPC(Remote Procedure Call) 是一个计算机通信协议.该协议允许运行与一台计算机的程序调用另一个地址空间的程序,是一个通过发送请求-接受回应进行信息交互的系统. ...
- ngx instance
首先看下 连接池的获取以及释放 ngx_connection_t * ngx_get_connection(ngx_socket_t s, ngx_log_t *log) //从连接池中获取一个ngx ...
- python3:文件读写+with open as语句(转)
读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘, ...
- 谷歌Colab使用(深度学习)
1. Coalb简介 Google Colaboratory是谷歌开放的一款研究工具,主要用于机器学习的开发和研究.这款工具现在可以免费使用,但是不是永久免费暂时还不确定.Google Colab最大 ...
- rgw实现nfs的首测
功能介绍 关于rgw实现nfs接口这个,刚接触的人可能并不清楚这个是个什么样的服务架构,rgw是ceph里面的对象存储接口,而nfs则是纯正的网络文件系统接口,这二者如何结合在一起,关于这个,有几个相 ...