C++ 中的 typedef 和 using

  1. 语法: typedef 的语法相对固定,而 using 在C++11之后提供了更灵活的方式,可以定义模板别名等。
  2. 模板别名: 在C++11之后,using 可以用来定义模板别名,而 typedef 则无法做到这一点
  3. 作用域: 在命名空间中使用 usingtypedef 有一些差异,using 更符合命名空间的作用域规则,typedef 则没有这种作用域概念。

总的来说,using 更加现代化且灵活,尤其在C++11之后,推荐使用它来定义别名。

模板别名的例子:

cpp 复制代码
template <typename T> using PromisePtr = RefPtr<Promise<T>>;
//使用 模板类型别名:
template <typename TRet>
PromisePtr<TRet> unwrap(Promise<TRet>* p)
{
    return p;
}
template <typename TRet>
PromisePtr<TRet> unwrap(PromisePtr<TRet> p)
{
    return p;
}
相关推荐
NiceCloud喜云5 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
cjhbachelor5 小时前
c++继承
c++
AI玫瑰助手6 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
油炸自行车6 小时前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
肩上风骋6 小时前
C++14特性
开发语言·c++·c++14特性
JAVA社区7 小时前
Java高级全套教程(十)—— SpringCloudAlibaba超详细实战详解
java·开发语言·spring cloud·面试·职场和发展
弥树子7 小时前
踩坑记录:服务器内网调用接口,真实请求URL与官方公开URL不一致问题排查
开发语言·php
z落落8 小时前
C# ToCharArray + foreach遍历 + String与StringBuilder
开发语言·c#
学代码的真由酱8 小时前
Java多用户一对一网页聊天室-测试报告
java·开发语言·功能测试·测试
人道领域8 小时前
【LeetCode刷题日记】669.修剪二叉搜索树
开发语言·python·算法