c++ - 泛型类的 Typedef(别名)

标签 c++ templates

在C++中是否可以创建模板类的别名(不指定参数)?

typedef std::map myOwnMap;

不起作用。

如果不是,有什么好的理由吗?

最佳答案

在 C++98 和 C++03 中,typedef 只能用于完整类型:

typedef std::map<int,int> IntToIntMap;

在 C++0x 中,有一个新的语法来替代 typedef:

using IntToIntMap = std::map<int,int>;

它也支持 template 别名:

template <
  typename Key,
  typename Value,
  typename Comparator = std::less<Key>,
  typename Allocator = std::allocator< std::pair<Key,Value> >
>
using myOwnMap = std::map<Key,Value,Comparator,Allocator>;

给你:)

关于c++ - 泛型类的 Typedef(别名),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3591024/

相关文章:

c++ - 空指针初始化?如果不是,那是什么?

c++ - 用时钟计时for循环

c++ - 插入以 boost 无序 map

c# - 在 C#/C++ 之间编码复杂结构

c++ - 在 C++ 中清除控制台

bash - 使用 bash 脚本从模板创建新文件

c++ - 使用模板实现通用字符串解析器

c++ - 从模板函数内部的类型中删除 const-ness

c++ - 规避模板特化

c++ - 为什么 C++1* 仍然需要 template 关键字来代替 Full Duck Typing