嗨,最近我想到了一个“简单的问题”,就是把代码从VC++移植到gcc/英特尔。代码在VC++上编译w/o错误:
#include <vector>
using std::vector;
template <class T>
void test_vec( std::vector<T> &vec)
{
typedef std::vector<T> M;
/*==> add here typename*/ M::iterator ib=vec.begin(),ie=vec.end();
};
int main()
{
vector<double> x(100, 10);
test_vec<double>(x);
return 0;
}然后,对于g++,我们有一些不清楚的错误:
g++ t.cpp
t.cpp: In function 'void test_vec(std::vector<T, std::allocator<_CharT> >&)':
t.cpp:13: error: expected `;' before 'ie'
t.cpp: In function 'void test_vec(std::vector<T, std::allocator<_CharT> >&) [with T = double]':
t.cpp:18: instantiated from here
t.cpp:12: error: dependent-name 'std::M::iterator' is parsed as a non-type, but instantiation yields a type
t.cpp:12: note: say 'typename std::M::iterator' if a type is meant如果在迭代器之前添加typename,代码将编译w/o。
如果有可能使编译器能够理解以更“自然的方式”编写的代码,那么对于我来说,为什么要添加typename呢?如果允许所有编译器不使用"typename“,那么"C++标准”的哪些规则(如果有的话)将被打破?
向阿曼问好。
发布于 2010-06-11 05:49:42
这里是一页解释了typename。
发布于 2010-06-11 05:56:01
http://msdn.microsoft.com/en-us/library/8y88s595(VS.71).aspx
这会帮到你..。
https://stackoverflow.com/questions/3020441
复制相似问题