当前位置:网站首页>decltype用法介绍
decltype用法介绍
2022-06-24 06:51:00 【GarryLau】
想从表达式推断出要定义的变量的类型,但不想计算表达式的值,此时可以使用decltype
语法是:delctype(表达式),其中表达式可以是变量、函数、数组等。
#include <typeinfo>
#include <iostream>
namespace test_decltype {
double onlyDeclartionFunc();
auto main() -> int {
std::cout << "testing decltype..." << std::endl;
/* 表达式是函数 */
decltype(onlyDeclartionFunc()) sum = 34; // 使用decltype根据函数类型推断类型时可以仅要求函数有声明,不要求函数有定义
std::cout << "type(sum) is: " << typeid(sum).name() << std::endl; // double
/***************************************************************************/
float i = 3.4f;
decltype(i) a = 52;
std::cout << "type(a) is: " << typeid(a).name() << std::endl; // float
// 使用decltype时会返回变量的真实类型(包括const和引用),这与auto有区别
const int ci = 0; // const int
const int &cj = ci; // const int &
decltype(ci) b = 9; // const int
// b = 10; // error C3892: “b”: 不能给常量赋值
decltype(cj) c = b; // const int &
// c = ci; // error C3892: “c”: 不能给常量赋值
decltype(cj) d = 9; // const int &
// decltype(cj) e; // error C2530: “e”: 必须初始化引用
std::cout << "type(ci) is: " << typeid(ci).name() << std::endl; // const int(ps:编译器输出时不会带const,下同)
std::cout << "type(cj) is: " << typeid(cj).name() << std::endl; // const int &
std::cout << "type(b) is: " << typeid(b).name() << std::endl; // const int
std::cout << "type(c) is: " << typeid(c).name() << std::endl; // const int &
std::cout << "type(d) is: " << typeid(d).name() << std::endl; // const int &
/***************************************************************************/
// decltype(表达式)推断出引用类型的几种情况:
// 1. 表达式本身是引用;
// 2. 表达式是指针的解引用;
// 3. 表达式加括号;
int j = 0;
int &k = j;
int *p = &j;
std::cout << "Original j, 0 == " << j << std::endl;
decltype(k) f = k; // f是j的引用(表达式本身是引用)
f = 1;
std::cout << "f is j's reference, 1 == " << j << std::endl;
decltype(*p) g = j; // g是j的引用(表达式是指针的解引用)
g = 2;
std::cout << "g is j's reference, 2 == " << j << std::endl;
decltype((j)) h = j; // h是j的引用(表达式加括号)
h = 3;
std::cout << "h is j's reference, 3 == " << j << std::endl;
decltype(k+0) m = k; // m是int,不是int&,因为k+0是int类型
m = 4;
std::cout << "m is not j's reference, 4 != " << j << std::endl;
// 对数组使用decltype**得到的是数组类型
int arr[] = {
3,4,5};
// decltype(arr) crr = {5,6,7,8,9}; // error: too many initializers for 'int [3]'
decltype(arr) drr = {
5,6,7}; // 注意,数组元素的个数是数组类型的一部分
std::cout << "type(drr) is: " << typeid(drr).name() << std::endl; // int [3]
/***************************************************************************/
std::cout << "------------------------------" << std::endl;
return 0;
}
}
以上程序的输出:
testing decltype...
type(sum) is: double
type(a) is: float
type(ci) is: int
type(cj) is: int
type(b) is: int
type(c) is: int
type(d) is: int
Original j, 0 == 0
f is j's reference, 1 == 1 g is j's reference, 2 == 2
h is j's reference, 3 == 3 m is not j's reference, 4 != 3
type(drr) is: int [3]
对数组使用decltype得到的是数组类型,与auto不同
// 对数组使用decltype
int arr[] = {
3,4,5};
// decltype(arr) crr = {5,6,7,8,9}; // error: too many initializers for 'int [3]'
decltype(arr) drr = {
5,6,7}; // 注意,数组元素的个数是数组类型的一部分
std::cout << "type(drr) is: " << typeid(drr).name() << std::endl; // int [3]
边栏推荐
- Teach you how to use the reflect package to parse the structure of go - step 2: structure member traversal
- Bit operation
- 某问答社区App x-zse-96签名分析
- Notes on the use of date and time base
- 毕业两年月薪36k,说难也不难吧
- How to cancel the display of the return button at the uniapp uni app H5 end the autobackbutton does not take effect
- . No main manifest attribute in jar
- Leetcode 174 Dungeon games (June 23, 2022)
- Pair class notes
- Jenkins 太老了 试试它?云原生 CI/CD Tekton
猜你喜欢

Baidu map, coordinate inversion, picking coordinate position

语料库数据处理个案实例(读取多个文本文件、读取一个文件夹下面指定的多个文件、解码错误、读取多个子文件夹文本、多个文件批量改名)

单片机STM32F103RB,BLDC直流电机控制器设计,原理图、源码和电路方案

面试中的最常被问到的两种锁
![LeetCode 515 在每个数行中找最大值[BFS 二叉树] HERODING的LeetCode之路](/img/16/011ba3aef1315c39526daac7e3ec89.png)
LeetCode 515 在每个数行中找最大值[BFS 二叉树] HERODING的LeetCode之路

Echart 心得 (一): 有关Y轴yAxis属性

How to cancel the display of the return button at the uniapp uni app H5 end the autobackbutton does not take effect

第 3 篇:绘制三角形

Moonwell Artemis现已上线Moonbeam Network

ImportError: cannot import name ‘process_ pdf‘ from ‘pdfminer. Pdfinterp 'error completely resolved
随机推荐
位运算
IndexError: Target 7 is out of bounds.
js实现查看一个数组对象中是否包含另一个数组对象中的值
Baidu map, coordinate inversion, picking coordinate position
第 2 篇:繪制一個窗口
Chapter 2 line graph of canvas
《canvas》之第4章 线条操作
Hongmeng development IV
Q & A on cloud development cloudbase hot issues of "Huage youyue phase I"
Practice of opengauss database on CentOS, configuration
Echart's experience (I): about y axis yaxis attribute
Tuple remarks
Mousse shares listed on Shenzhen Stock Exchange: gross profit margin continued to decline, and marketing failed in the first quarter of 2022
单片机STM32F103RB,BLDC直流电机控制器设计,原理图、源码和电路方案
Specify IP when calling feign interface
Los Angeles p1051 who won the most Scholarships
duilib 显示内存图片
报错“Computation failed in `stat_summary_hex()`”
New features of PHP: bytecode cache and built-in server
JS implementation to check whether an array object contains values from another array object