首页 > 其他分享 >typedef vs using

typedef vs using

时间:2023-12-20 10:35:30浏览次数:28  
标签:std Map typedef map 别名 vs using

语言支持类型别名typedef,显然,CPP也支持咯。不过自CPP11(也称之为Modern CPP)开始,引入了using关键字用以表示类型别名。

创建类型别名

typedefusing都可以创建类型别名,区别是在语法语义上的不同。

typedef的语法如下:

typedef [original-type] [alias];

用法如下:

typedef  int MyInt;
typedef std::map<std::string, std::vector<std::string>> Map;

MyInt a = 3;
Map m;

using的语法如下:

using [alias] = [original-type];

用法如下:

using MyInt = int;
using Map = std::map<std::string, std::vector<std::string>>;

MyInt a = 3;
Map m;

从上述示例看出,无论是使用typedef还是using都创建一个类型别名,他们的使用方式是一样的。也就是说到目前为止,看起来 typedef 和用法是相同的,但存在即合理,对using的引入肯定有其他原因,这就引入了typedef和using在模板别名上的不同。

创建模板别名

在前面的例子中,通过typedef和using创建了std::map<std::string, std::vector<std::string>>的别名Map,但是这个Map的类型是固定的,也就是说只能应用于key是string,value为std::vector<std::string>的map,如果要创建一个std::map<int, std::vector<int>>的别名,就需要重新声明,就像下面这种:

typedef std::map<int, std::vector<int>> Map1;
using Map2 = std::map<int, std::vector<int>>;

显然,这张重新声明别名的方式在通用性上很差,所以为了解决通用性问题,就需要使用template

幸运的是,using可以直接创建模板别名:

template<[template-parameter-list]> using [your-alias] = [original-type];

比如:

template<typename T1, typename T2> using Map = std::map<T1, std::vector<T2>>;
Map<int, int> m;

此时,你可能会问typedef是不是也可以创建模板类型别名?当然可以,但是相对于using,typedef创建模板类型别名会麻烦很大。

如果要使用typedef创建模板类型别名,就需要创建一个模板类,这个别名的声明需要封装在结构体中,如下:

template<typename T1, typename T2>
struct KV{
  typedef std::map<T1, std::vector<T2>> type;
};

KV<int, std::string>::type Map;

结语

在此,我们引入标准中的一句话作为本文的结语:

A typedef-name can also be introduced by an alias-declaration. The identifier following the *using* keyword becomes a typedef-name and the optional attribute-specifier-seq following the identifier appertains to that typedef-name. It has the same semantics as if it were introduced by the typedef specifier. In particular, it does not define a new type and it shall not appear in the type-id.

也就是说在typedef和using在语义是一样的,只是后者的引入更为方便使用而已。

typedef vs using

标签:std,Map,typedef,map,别名,vs,using
From: https://www.cnblogs.com/blizzard8204/p/17915916.html

相关文章

  • cpp环境搭建 - vs2017编译CMakeLists项目(Box2dLite)
    box2dlite地址:GitHub-erincatto/box2d-lite:Asmall2Dphysicsengine vs2017不支持utf-8withoutbom问题box2dlite的源码文件是utf-8withoutbom的,如果在里面写了中文注释,就会出现编译错误解决办法:将文件编码改成utf-8带bom的(这边没有在附加选项加/utf-8貌似也没问题......
  • clang VS gcc 的command-line机制: clang 在 MacOS 上要设置 -isysroot $(xcrun --sho
    clangVSgcc的command-line机制:clang在MacOS上作为编译器时要设置-isysroot$(xcrun--show-sdk-path)注意明确指定clang/clang++在MacOS上作为编译器时,一定要设置CFLAGS/CPPFLAGS为"-isysroot$(xcrun--show-sdk-path)${CFLAGS}"CC="/usr/local/bin/clang"C......
  • vscode调试apollo
    一、插件要求二、通过插件进入容器三、配置默认进入的目录和用户如果不配置默认用户是root,运行不了apollo程序。 修改红框中的默认工作目录和用户名{ "workspaceFolder":"/apollo", "remoteUser":"chenjian", "extensions":[ "BazelBuild.vscode-bazel", &q......
  • vscode中Todo Tree插件的使用
    vscode中TodoTree插件的使用配置JSON将下方的JSON代码放入用户配置中复制JSON配置后,点击这里,然后粘贴。"todo-tree.tree.showScanModeButton":false,"todo-tree.filtering.excludeGlobs":["**/node_modules","*.xml","*.XML"],"todo......
  • vscode插件,将修改的文件同步到服务器
    安装扩展--deploy 在setting.json中进行设置 "deploy":{"targets":[{"type":"sftp","name":"MySFTPfolder","descr......
  • SharedFlow vs StateFlow,一篇看懂选择和使用技巧
    引言在Android应用开发中,数据流是一个至关重要的概念。而在Jetpack库中,SharedFlow和StateFlow是两个处理数据流的利器,它们基于协程,提供了一种响应式的编程方式。本文将深入探讨这两个类的原理,以及在实际开发中的使用技巧。原理分析SharedFlow和StateFlow基于协程构建,它们利用......
  • printf, fprintf, sprintf, vsprintf, snprintf, vsnprintf之间的用法和区别
    1、printf是打印格式化字符串,返回值是成功打印的字符数;intprintf(constchar*format,...);  2、fprintf是将格式化数据写入到指定文件流中;intfprintf(FILE*stream,constchar*format,...);  3、sprintf是将格式化的数据写入到指定字符串;intsprintf(char*str,......
  • 文心一言 VS 讯飞星火 VS chatgpt (160)-- 算法导论12.4 2题
    二、用go语言,请描述这样一棵有n个结点的二叉搜索树,其树中结点的平均深度为O(lgn),但这棵树的高度是w(lgn)。一棵有n个结点的二叉搜索树中结点的平均深度为O(lgn),给出这棵树高度的一个渐近上界。文心一言:二叉搜索树(BinarySearchTree,BST)是一种特殊的二叉树,它对于每个节点都......
  • IDE之VS:Visual Studio的简介(包括 VS2013、VS2015、VS2017、VS2019、VS2022)、安装、
    原文链接:https://blog.csdn.net/qq_41185868/article/details/81052119最近开始使用vs2019,应该是最新的版本。之前都是vs2015,感觉19更智能,兼容性更好,速度也更快。详细了解下这几个版本。1、简介:MicrosoftVisualStudio(简称VS)是美国微软公司的开发工具包系列产品,功能完备的I......
  • 【Windows】微软自带的系统阴影副本(VSS)
    学习就是在生活的方方面面!在前两天和我的老哥的一次闲聊中,老哥介绍了Windows系统的NTFS磁盘自带的,类似于快照的系统,VSS于是我就开始了学习! 1.什么是VSSVSS是微软公司的一种技术,全称为VolumeShadowCopyService(卷影复制服务)。VSS是Windows操作系统中的一个功能,用于创建文......