【发布时间】:2010-07-08 09:40:29
【问题描述】:
再一次,我希望 C++ 有更强的typedefs:
#include <vector>
template<typename T>
struct A {
typedef std::vector<T> List;
};
template<typename T>
void processList(typename A<T>::List list) {
// ...
}
int main() {
A<int>::List list;
processList<int>(list); // This works.
processList(list); // This doesn't.
}
显然,编译器将list 视为std::vector<int> 而不是A<int>::List,因此无法将其与预期的A<T>::List 匹配。
在实际情况下,它是一个较长的类型名称,经常重复,而且很麻烦。除了让processList 接受vector 之外,还有什么方法可以让模板类型推断对我有用吗?
【问题讨论】:
标签: c++ templates type-inference