2016-02-25 35 views
2

我有具有取決於類型作爲用作函數參數的typedef模板類依賴型爲函數參數的聲明在類Foo之外正確聲明功能bar()的意見。在模板類

我originately寫這樣在Linux上,這GCC愉快編譯:

template <typename T> 
inline void Foo<T>::bar(const Foo<T>::Iterator& it) const {} 

現在試圖編譯上的Visual Studio 2015年,我得到的

1>main.cpp(14): error C2065: 'it': undeclared identifier 
1>main.cpp(14): warning C4346: 'Foo<T>::Iterator': dependent name is not a type 
1> main.cpp(14): note: prefix with 'typename' to indicate a type 

所以出現以下錯誤消息該VS要求typename關鍵字出現在Iterator之前。精細。我添加了它,但現在它拒絕在GCC中編譯。

main.cpp:14:35: error: variable or field 'bar' declared void 
inline void Foo<T>::bar(typename const Foo<T>::Iterator& it) const {} 

你可以看到它住在這裏http://ideone.com/EaUeus

所以我的問題是......誰是正確的?有沒有辦法讓這個代碼在兩個平臺上編譯而不需要使用#ifdef

回答

2

您應該使用const typename而不是typename const。編譯器無法知道Foo<T>::Iterator是一種類型,而const只能用於類型。所以首先說這是一種類型,然後使其成爲const

+0

啊,我應該想到這件事的人,你是對的。接得好。 – Louen