2010-06-11 48 views
0

可能重複:
Why do I need to use typedef typename in g++ but not VS?學術問題:TYPENAME

你好,最近我佔與VC++代碼移植到GCC /英特爾的 「簡單問題」。 的代碼編譯W/O錯誤在VC++:

#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++ 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 

如果我們迭代器的代碼編譯的w/o前加類型名 PB。

如果有可能使一個編譯器能夠理解以更自然的方式編寫的代碼,那麼對於我來說就不清楚爲什麼我們應該添加typename?如果我們允許所有編譯器在沒有「typename」的情況下使用,那麼「C++標準」(如果有的話)中的哪些規則會被打破?

親切的問候 阿曼。

回答

2

這裏的is a page說明了typename

+0

這就是我一直在尋找,謝謝。 – Arman 2010-06-11 06:02:01

+0

你應該在這裏寫一個摘要,這樣1)我們有一個理由來讚揚答案,2)所以即使你鏈接的頁面關閉,答案仍然有用。 – jalf 2010-06-11 07:14:22