【Effective Modern C++】第三章 转向现代C++:9. 优先选用别名声明,而非typedef

具有两种声明别名的方法:

c++ 复制代码
typedef std::unique_ptr<std::unordered_map<std::string, std::string>> UPtrMapSS; 

using UPtrMapSS = std::unique_ptr<std::unordered_map<std::string, std::string>>;

别名声明在处理涉及函数指针的类型时比较容易理解:

c++ 复制代码
//FP是一个指向函数的指针的同义词,它指向的函数带有int和const std::string&形参,不返回任何东西
typedef void (*FP)(int, const std::string&);    //typedef

//含义同上
using FP = void (*)(int, const std::string&);   //别名声明

别名声明可以模板化(被称为别名模板),而typedef不行:

使用别名声明:

c++ 复制代码
template<typename T> 
using MyAllocList = std::list<T,MyAlloc<T>>; // MyAllocList<T>是std::list<T,MyAlloc<T>>的同义词

MyAllocList<Widget> lw; // 客户代码

使用typedef需要从头开始:

c++ 复制代码
template<typename T>                            
struct MyAllocList {                           
    typedef std::list<T, MyAlloc<T>> type;      
}; //MyAllocList<T>是std::list<T, MyAlloc<T>>的同义词

MyAllocList<Widget>::type lw; // 客户代码

模板内使用的差异:typename 的必要性

模板内引用嵌套typedef的类型时,必须加typename前缀(因MyAllocList<T>::type是依赖类型 ------ 编译器无法确定特化版本中::type是否为类型):

c++ 复制代码
template<typename T>
class Widget {
private:
    // 必须加typename:MyAllocList<T>::type依赖于T,可能被特化为非类型(如数据成员)
    typename MyAllocList<T>::type list; 
};

// 特化示例:MyAllocList<Wine>::type不是类型,而是数据成员
class Wine {};
template<>
class MyAllocList<Wine> {
private:
    enum class WineType { White, Red, Rose };
    WineType type; // 此处type是数据成员,非类型别名
};

而别名模板无需typename(编译器明确其结果必为类型,属于非依赖类型):

c++ 复制代码
template<typename T>
class Widget {
private:
    MyAllocList<T> list; // 无typename、无::type,简洁且无歧义
};

类型特性(type traits)场景的适配

C++11 的 type traits(如类型转换)基于struct嵌套typedef实现,使用时需::type后缀 + 模板内typename:

c++ 复制代码
// C++11:需::type后缀,模板内还需加typename
std::remove_const<T>::type;          // 移除const修饰
std::remove_reference<T>::type;      // 移除引用修饰
std::add_lvalue_reference<T>::type;  // 添加左值引用

C++14 提供了基于别名声明的简化版本(后缀_t),无需::type和typename;即便仅支持 C++11,也可手动仿写这些别名模板:

c++ 复制代码
// C++14简化版
std::remove_const_t<T>;              // 等价于std::remove_const<T>::type
std::remove_reference_t<T>;          // 等价于std::remove_reference<T>::type

// C++11手动仿写别名模板(兼容方案)
template <class T> 
using remove_const_t = typename std::remove_const<T>::type;

总结

  • typedef不支持模板化,但别名声明支持。
  • 别名模板可以让人免写::type后缀,并且在模板内,对于内嵌typedef的引用经常要求加上typename前缀。

原著在线阅读地址

相关推荐
繁星蓝雨5 小时前
C++设计原理———多重继承(虚基类、菱形继承、委托、重命名、基类和成员初始化)
c++·委托·多重继承·mi·显式初始化列·虚基类·对象布局
旖旎夜光5 小时前
【C++ 基础入门】命名空间、输入输出、缺省参数与函数重载
开发语言·c++·笔记·学习
扶尔魔ocy6 小时前
【QT入门到晋级】用开源的自定义控件开发
c++·qt·自定义控件
akinggw8 小时前
libsherpacore - 基于 sherpa-onnx 的 C++ 语音交互静态库
c++·交互·语音识别
xiaoye-duck8 小时前
《C++ 项目:手写大模型接入 SDK》ChatGPT接口设计(中):全量响应实现和测试
网络·c++·语言模型·chatgpt
東隅已逝,桑榆非晚9 小时前
算法3.双指针法-快乐数
c++·笔记·学习·算法
eggwyw9 小时前
C++相交链表和反转链表详解
开发语言·c++
杜 硕9 小时前
从零开始的C++生活——第六讲——02
c++·算法·生活
小超人诞生日记9 小时前
C++ STL 反向迭代器:从源码框架到适配器实现
c++·算法
青少儿编程课堂10 小时前
蓝桥杯青少组图形化 + 全国青少年信息素养大赛 + 电子学会Scratch考级:校园成语接龙大挑战解析
c++·python·算法·bfs·信息学竞赛