decltype有什么用_剑本身用法的介绍

decltype有什么用_剑本身用法的介绍想从表达式推断出要定义的变量的类型,但不想计算表达式的值,此时可以使用decltype

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

想从表达式推断出要定义的变量的类型,但不想计算表达式的值,此时可以使用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]
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/195474.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python 字符转义(url中文转义)

    URL特殊字符需转义1、空格换成加号(+)2、正斜杠(/)分隔目录和子目录3、问号(?)分隔URL和查询4、百分号(%)制定特殊字符5、#号指定书签6、&号分隔参数转义字符的原因:如果你的表单使用get方法提交,并且提交的参数中有“&”等特殊符的话,如果不做处理,在service端就会将&后面的作为另外一个参数…

    2022年4月14日
    79
  • C语言 u16_c语言自定义数据类型

    C语言 u16_c语言自定义数据类型stdint.h这里放着C语言的标准表达方式typedefsignedcharint8_t;typedefsignedshortintint16_t;typedefsignedintint32_t;typedefsigned__int64int64_t;typedefun…

    2022年10月15日
    4
  • Xposed模块制作入门「建议收藏」

    Xposed模块制作入门「建议收藏」由于某个项目的特殊需求,可能要用Xposed框架干一些事情。然而在国内基本没有找到关于Xposed模块制作的文章(基本上全是下载呵呵),只在XDA上找到一篇教程。这篇文章差不多是XDA上的教程翻译过来的(原文链接:http://forum.xda-developers.com/showthread.php?t=2709324可能需科学上网) 首先,Xposed框架是干啥的捏?玩安卓机比较多的

    2026年1月28日
    3
  • pycharm的scrapy框架-断点调试「建议收藏」

    pycharm的scrapy框架-断点调试「建议收藏」在文件根目录,也就是settings.py的上级目录,scrapy.cfg的同级目录,创建main.py:fromscrapy.cmdlineimportexecuteimportosimportsysif__name__==’__main__’:sys.path.append(os.path.dirname(os.path.abspath(__file__)))execute([‘scrapy’,’crawl’,’你的spider的name’])点

    2022年5月11日
    46
  • MySQL字符串拼接函数介绍

    MySQL字符串拼接函数介绍在 MySQL 中 实现字符串拼接主要有以下 3 种函数 concat x y concat ws 分隔符 x y group concat distinctxxxo descyyysepar 分隔符 一 concat 函数 concat 函数用于将多个字符串连接成一个字符串格式 concat str1 str2 mysqlconca

    2025年6月9日
    3
  • 网页音乐播放器接口

    网页音乐播放器接口1、Dewplayer音乐播放器2、我的博客首页虾米播放器代码:<embedsrc=”http://www.xiami.com/widget/0_1769834090/singlePlayer.swf”type=”application/x-shockwave-flash”width=”257″height=”33″wmode=”tra…

    2022年6月22日
    37

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号