如何识别Visual Studio中缺少的类型名

How to identify missing typename in Visual Studio

本文关键字:类型 Studio Visual 何识别 识别      更新时间:2023-10-16

是否存在标识VS中缺少的类型名的方法?VS至少会产生某种警告吗?

template<class T> 
void dum() {
  std::vector<T> dum_vector;
  typename std::vector<T>::iterator it = dum_vector.begin(); 
  // VS compiles it with or without typename, but I would like to know whether 
  // I forgot to put a typename, since without typename the code may not compile 
  // with another compiler (e.g. GCC)
}

我不确定它是否具有100%的标准一致性,但对于明确需要typename的所有或大多数情况,MSVC都会生成编译器警告(级别1)C4346。因此,只要您使用编译器标志/W1或更高版本进行编译,您就应该没事。

实际上,在当前版本的C++(即C++11)中,您不需要写那么多。你可以这样写:

auto it = dum_vector.begin(); 

你就完了。

请注意,从MSVC10开始就支持auto,所以如果您正在使用它,我建议您使用auto来代替blah::blah::iterator。如果您使用的是旧版本,最好升级并尽可能利用C++11功能的优势。如果您不能做到这一点,那么MSVS不太可能告诉您缺少的类型名,因为编译器首先编译的是非标准代码!

相关文章: