c++ auto 属性
auto 指定符(C++11 起)
对于变量,指定其类型将从其初始化器自动推导而出。
对于函数,指定其返回类型是尾随的返回类型或将从其 return 语句推导出 (C++14 起)。
对于非类型模板形参,指定其类型将从参数推导出。 (C++17 起)
语法
auto variable initializer | (1) | (C++11 起) | |||||||
auto function -> return type | (2) | (C++11 起) | |||||||
auto function | (3) | (C++14 起) | |||||||
decltype(auto) variable initializer | (4) | (C++14 起) | |||||||
decltype(auto) function | (5) | (C++14 起) | |||||||
auto ::
|
(6) | (概念 TS) | |||||||
cv(可选) auto ref(可选) parameter | (7) | (C++14 起) | |||||||
template < auto Parameter >
|
(8) | (C++17 起) | |||||||
cv(可选) auto ref(可选) [ identifier-list ] initializer ;
|
(9) | (C++17 起) | |||||||
解释
只要初始化器的类型被确定,则编译器会用来自函数调用的模板实参推导规则所决定的类型替换关键词 auto (细节见模板实参推导)。关键词 auto 可与修饰符组合,如 const 或 & ,它们将参与类型推导。例如,给出 const auto& i = expr; , i
的类型恰是虚构模板 template<class U> void f(const U& u) 中参数 u
的类型,倘若函数调用 f(expr) 被编译。从而, auto&& 可根据初始化器被推导成左值或是右值引用,这被用于基于范围的for循环。
若 auto 被用于声明多个变量,则推导类型必须相符。例如,声明 auto i = 0, d = 0.0; 为病式,而声明 auto i = 0, *p = &i; 为良式,这里 auto 被推导成int。
auto
,则其类型由对应使用参数推导出。
注意
C++11 前, auto 拥有存储期指定符的语义。
不允许在一个声明中混合 auto
变量和函数,如 auto f() -> int, i = 0; 。
示例
#include <iostream> #include <utility>template<class T, class U> auto add(T t, U u) { return t + u; } // 返回类型是 operator+(T, U) 的类型 // 在其所调用的函数返回引用的情况下 // 函数调用的完美转发必须用 decltype(auto) template<class F, class... Args> decltype(auto) PerfectForward(F fun, Args&&... args) { return fun(std::forward<Args>(args)...); } template<auto n> // C++17 auto 形参声明 auto f() -> std::pair<decltype(n), decltype(n)> // auto 不能从花括号初始化器列表推导 { return {n, n}; } int main() { auto a = 1 + 2; // a 的类型是 int auto b = add(1, 1.2); // b 的类型是 double static_assert(std::is_same_v<decltype(a), int>); static_assert(std::is_same_v<decltype(b), double>); decltype(auto) c1 = a; // c1 的类型是 int ,保有 a 的副本 decltype(auto) c2 = (a); // c2 的类型是 int& ,为 a 的别名 std::cout << "a, before modification through c2 = " << a << '\n'; ++c2; std::cout << "a, after modification through c2 = " << a << '\n'; auto [v, w] = f<0>(); // 结构化绑定声明 auto d = {1, 2}; // OK : d 的类型是 std::initializer_list<int> auto n = {5}; // OK : n 的类型是 std::initializer_list<int> // auto e{1, 2}; // C++17 起错误,之前为 std::initializer_list<int> auto m{5}; // OK : C++17 起 m 的类型为 int ,之前为 initializer_list<int> // decltype(auto) z = { 1, 2 } // 错误: {1, 2} 不是表达式 // auto 常用于无名类型,例如 lambda 表达式的类型 auto lambda = [](int x) { return x + 3; }; // auto int x; // 于 C++98 合法, C++11 起错误 // auto x; // 于 C 合法,于 C++ 错误 }
可能的输出:
a, before modification through c2 = 3 a, after modification through c2 = 4
转载于:https://www.cnblogs.com/jlmgary/p/9111839.html
c++ auto 属性相关推荐
- css中的auto属性
css中auto的用法 -什么是auto? +auto是自适应的意思,auto是很多尺寸值的默认值,也就是由浏览器自动计算. +块级元素中margin.border.padding以及content宽 ...
- css margin属性 auto,css中margin:auto属性的使用方法
css中margin:auto属性的使用方法 发布时间:2020-06-25 10:53:17 来源:亿速云 阅读:221 作者:Leah 今天就跟大家聊聊有关css中margin:auto属性的使用 ...
- hibenate.hbm2ddl.auto属性详解
hibernate配置属性中,hibernate.hbm2ddl.auto可以帮助你实现正向工程,即由java代码生成数据库脚本,进而生成具体的表结构. 在hibernate.cfg.xml ...
- css中auto属性值的使用
理解前须知: width/ height的默认值为auto margin的默认为0 padding的默认为0 left right top bottom 默认值为auto border 不写属性的情况 ...
- html中auto是设置什么的,css中margin:auto什么意思?margin:auto属性的用法详解
我们都知道使用margin:auto可以让元素水平居中的.但你有没有想过使用margin:auto可以让元素水平居中的原因,要回答这个问题,我们首先需要看一下margin:auto的工作原理.auto ...
- html中zoom方法,CSS中zoom属性或overflow:auto的使用方法
这次给大家带来CSS中zoom属性或overflow:auto的使用方法,使用CSS中zoom属性或overflow:auto的注意事项有哪些,下面就是实战案例,一起来看一下. 前言 其实在CSS中的 ...
- CSS:margin属性的auto值
CSS margin的auto属性: 当使用flex布局是,若此行还有空余空间,则平均分配给有margin属性为auto的元素 参考:https://developer.mozilla.org/zh- ...
- 字段和属性之间有什么区别?
在C#中,是什么使字段不同于属性,以及何时应使用字段代替属性? #1楼 (这实际上应该是评论,但我不能发表评论,因此,如果它不适合发表,请原谅). 我曾经在一个推荐的实践是在公共属性而不是属性的地方工 ...
- Webkit Flex伸缩盒模型属性备忘
一个元素被定义display:-webkit-flex;则此元素即为伸缩盒模型中的伸缩容器,其包含的子元素是伸缩项目. flex是一个复合属性.Flexbox伸缩布局盒模型中的伸缩容器及伸缩特性定义. ...
- js锚点定位_overflow属性详解,利用CSS实现锚点定位
1.overflow的裁剪界线--border-box overflow属性用于指定块容器元素的内容溢出时的表现方式--滚动,裁剪,自适应."BFC的最佳结界"只是其衍生出来的特性 ...
最新文章
- 小猿圈Linux基础面试题,看看你能答对几道?
- 10 个神奇的网站显示超强的 HTML5 技术
- 洛谷 P3157 [CQOI2011]动态逆序对 | CDQ分治
- java 序列化 clone_利用java序列化进行对象深Clone
- copyonwritearraylist原理_Java集合干货——CopyOnWriteArrayList源码分析
- matlab中的:的优先级_内容早期设计:内容优先
- linux 高可用----keepalived+lvs
- 神经网络与深度学习——TensorFlow2.0实战(笔记)(五)(Matplotlib绘图基础<散点图>python)
- 快过年了,用JS让你的网页放烟花吧
- java多线程中 锁 的概念的理解,java 并发多线程显式锁概念简介 什么是显式锁 多线程下篇(一)...
- java中if条件中删除此行代码_Java中我如何去除if...else...语句?
- Map集合-根据宠物昵称查找宠物
- Spring Boot + Web Socket 实现扫码登录,这种方式太香了!!
- jquery 调用ajax返回json
- android ion --system heap(个人理解,不确定完全对)
- 财经大数据可视化Vdc平台_为何要使用大数据可视化平台
- Word2016写论文之尾注功能——参考文献自动编号与引用(包括方括号去除方法)
- 谷粒学苑上传视频踩坑
- mysql库函数说明_MySQL 数据库函数库
- 票啦啦--手机购电影票
热门文章
- python-gui-pyqt5的使用方法-4--自定义信号的初识--多参数的使用
- FR公式形态定义及运用范例
- python中print说法正确的是_python中的print()输出
- python折线图实线虚线_python – matplotlib中的虚线而不是缺失值
- CS224N笔记——RNN和语言模型
- 引入ELK前需要知道的“坑”(上)
- Linux之进程管理基础概念
- 解决 xshell 上面 zsh 的 home end 无效问题
- Linux安装MATLAB Compiler Runtime操作
- linux入门和简单应用举例