自从 GCC 发现我已经有一段时间了,但它就发生在今天。但我从来不明白为什么 GCC 需要在模板中使用 typedef typename,而 VS 和我猜想 ICC 不需要。typedef typename 是“错误”还是过于严格的标准,还是留给编译器编写者的东西?
对于那些不知道我的意思的人,这里有一个示例:
template<typename KEY, typename VALUE>
bool find(const std::map<KEY,VALUE>& container, const KEY& key)
{
std::map<KEY,VALUE>::const_iterator iter = container.find(key);
return iter!=container.end();
}
上面的代码在 VS 中编译(可能在 ICC 中),但在 GCC 中失败,因为它想要这样:
template<typename KEY, typename VALUE>
bool find(const std::map<KEY,VALUE>& container, const KEY& key)
{
typedef typename std::map<KEY,VALUE>::const_iterator iterator; //typedef typename
iterator iter = container.find(key);
return iter!=container.end();
}
注意:这不是我正在使用的实际功能,而只是演示问题的一些愚蠢的东西。