是否可以size_type从容器中获取类型别名,例如std::vector或std::map不提供value_type某种类型的 a ?
我正在编写一个程序,我需要size_type两种不同map类型的数据。但是,如果我为第一个映射类型创建 typedef,则该名称size_type对于第二个映射类型不可用。我的解决方法(又名“拼凑”)解决方案是说谎。
std::map<char, int> letters;
std::map<std::string, int> digraphs, trigraphs;
// This lie is good enough for both map types.
using size_type = typename std::map<int, int>::size_type;
Run Code Online (Sandbox Code Playgroud)
在其他情况下,我只是不想提供value_type. 这是一个人为的例子。
// Yes, I know this can be cleaned up by doing it in stages, but ugh!
using size_type = typename std::map<std::string, std::pair<std::string, std::string>>::size_type;
Run Code Online (Sandbox Code Playgroud)
最终,我们都知道size_type可能会成为 的别名std::size_t,即使在诸如 之类的极端情况下也是如此std::vector<bool>。尽管如此,我仍然尝试使用“正确”的方式做事size_type。 …