当前位置:网站首页>Decltype usage introduction
Decltype usage introduction
2022-06-24 08:00:00 【GarryLau】
Want to infer from the expression the type of variable to be defined , But I don't want to evaluate the expression , You can use decltype
Grammar is :delctype( expression ), Where the expression can be Variable 、 function 、 Array etc. .
#include <typeinfo>
#include <iostream>
namespace test_decltype {
double onlyDeclartionFunc();
auto main() -> int {
std::cout << "testing decltype..." << std::endl;
/* Expressions are functions */
decltype(onlyDeclartionFunc()) sum = 34; // Use decltype When inferring a type from a function type, you can only require the function to have a declaration , Functions are not required to be defined
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
// Use decltype Will return the real type of the variable ( Include const And quotes ), This is related to auto There's a difference
const int ci = 0; // const int
const int &cj = ci; // const int &
decltype(ci) b = 9; // const int
// b = 10; // error C3892: “b”: Constant cannot be assigned
decltype(cj) c = b; // const int &
// c = ci; // error C3892: “c”: Constant cannot be assigned
decltype(cj) d = 9; // const int &
// decltype(cj) e; // error C2530: “e”: Reference... Must be initialized
std::cout << "type(ci) is: " << typeid(ci).name() << std::endl; // const int(ps: The compiler will not output with const, The same below )
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( expression ) Infer several cases of reference types :
// 1. The expression itself is a reference ;
// 2. An expression is a dereference of a pointer ;
// 3. Expression parenthesized ;
int j = 0;
int &k = j;
int *p = &j;
std::cout << "Original j, 0 == " << j << std::endl;
decltype(k) f = k; // f yes j References to ( The expression itself is a reference )
f = 1;
std::cout << "f is j's reference, 1 == " << j << std::endl;
decltype(*p) g = j; // g yes j References to ( An expression is a dereference of a pointer )
g = 2;
std::cout << "g is j's reference, 2 == " << j << std::endl;
decltype((j)) h = j; // h yes j References to ( Expression parenthesized )
h = 3;
std::cout << "h is j's reference, 3 == " << j << std::endl;
decltype(k+0) m = k; // m yes int, No int&, because k+0 yes int type
m = 4;
std::cout << "m is not j's reference, 4 != " << j << std::endl;
// Use... For arrays decltype** The result is the array type
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}; // Be careful , The number of array elements is part of the array type
std::cout << "type(drr) is: " << typeid(drr).name() << std::endl; // int [3]
/***************************************************************************/
std::cout << "------------------------------" << std::endl;
return 0;
}
}
Output of the above program :
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]
Use... For arrays decltype The result is the array type , And auto Different
// Use... For arrays 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}; // Be careful , The number of array elements is part of the array type
std::cout << "type(drr) is: " << typeid(drr).name() << std::endl; // int [3]
边栏推荐
- 语料库数据处理个案实例(读取多个文本文件、读取一个文件夹下面指定的多个文件、解码错误、读取多个子文件夹文本、多个文件批量改名)
- 交友相亲类软件是如何割你韭菜的
- Moonwell Artemis is now online moonbeam network
- The startup mode of cloudbase init is \Cloudbase init has hidden dangers
- From jsonpath and XPath to spl
- Los Angeles p1051 who won the most Scholarships
- 用Ngrok 配置属于自己的免费外网域名
- 【资料上新】迅为基于3568开发板的NPU开发资料全面升级
- . No main manifest attribute in jar
- 面试中的最常被问到的两种锁
猜你喜欢

调用Feign接口时指定ip

Case examples of corpus data processing (cases related to sentence retrieval)

Vulnhub靶机:BOREDHACKERBLOG: SOCIAL NETWORK

Part 2: drawing a window

免费ICP域名备案查接口

云开发谁是卧底小程序源码

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

Signature analysis of app x-zse-96 in a Q & a community

第 1 篇:搭建OpenGL环境

Practice of opengauss database on CentOS, configuration
随机推荐
Open cooperation and win-win future | Fuxin Kunpeng joins Jinlan organization
The two most frequently asked locks in the interview
Pair class notes
L1-019 who goes first (15 points)
Detailed explanation of PHP data serialization test example
SCM stm32f103rb, BLDC DC motor controller design, schematic diagram, source code and circuit scheme
调用Feign接口时指定ip
随机数备注
ImportError: cannot import name ‘process_ pdf‘ from ‘pdfminer. Pdfinterp 'error completely resolved
Timer usage notes
某问答社区App x-zse-96签名分析
Echart's experience (I): about y axis yaxis attribute
Exness: Powell insisted on his anti inflation commitment and pointed out that recession is possible
GraphMAE----论文快速阅读
第 1 篇:搭建OpenGL环境
What is the lifecycle of automated testing?
Free ICP domain name filing interface
5-if语句(选择结构)
opencvsharp二值图像反色
Part 1: building OpenGL environment