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的例子. 类模板的非类型模板参 ...
随机推荐
- Java安全之Commons Collections7分析
Java安全之Commons Collections7分析 0x00 前言 本文讲解的该链是原生ysoserial中的最后一条CC链,但是实际上并不是的.在后来随着后面各位大佬们挖掘利用链,CC8,9 ...
- Cobalt Strike使用的一些技巧
利用msf模块上线beacon shell 当通过CS的mimikatz或者其他方式获得了目标机器的明文密码或者哈希时,可以利用metasploit的psexec_command模块来上线CS的bea ...
- APIview + Serializers
1.APIview使用 https://www.cnblogs.com/xiaonq/p/10124104.html https://www.cnblogs.com/xiaonq/p/109878 ...
- 自定义控件-只有横线的文本输入框(TxtLine)
需求:在实际开发中,由于TextBox控件的背景色样式不美观,且TextBox不能直接设置背景颜色,因此需要使用自定义控件以实现如下效果 实现代码 public partial class TxtLi ...
- Nodejs在VSCode下代码智能提示
在学习Nodejs的过程中发现vscode下默认没有提示,在网上也测试了传统的一些方法,都不好用,最后找到这个npm install --save-dev @types/node
- 833. Find And Replace in String —— weekly contest 84
Find And Replace in String To some string S, we will perform some replacement operations that replac ...
- Python调用飞书发送消息
一.创建飞书机器人 自定义飞书机器人操作步骤,具体详见飞书官方文档:<机器人 | 如何在群聊中使用机器人?>
- pip升级失败
python -m pip install --upgrade pip失败 解决办法: easy_install pip
- Spark架构与原理这一篇就够了
一.基本介绍 是什么? 快速,通用,可扩展的分布式计算引擎. 弹性分布式数据集RDD RDD(Resilient Distributed Dataset)弹性分布式数据集,是Spark中最基本的数据( ...
- stm32与地磁传感器HMC5883L
1.简介 霍尼韦尔 HMC5883L 是一种表面贴装的高集成模块,并带有数字接口的弱磁传感器芯片,应用于低成本罗盘和磁场检测领域.HMC5883L 包括最先进的高分辨率 HMC118X 系列磁阻传感器 ...