存在我有這種情況發生,以具有相同的名稱爲一些類的成員函數模板類模板調用成員函數模板。現在另一個函數模板被其中一個具有成員函數模板的類實例化。要調用此函數模板中的成員函數模板,我需要使用template
關鍵字,我理解這一點,並且沒有任何問題。不過,我需要使用範圍解析運算符(我剛剛發現,就是這就是所謂的)::
指定我的意思是類的成員函數模板,而不是類模板,我不明白爲什麼。需要範圍解析運算符時模板類具有相同名稱的
這是一個很大的模板化的東西,所以讓我舉一個例子:
//class with same name as member function below.
//must be class template or error doesn't show up.
//also no error if this is a function template instead of class
template <class T>
struct f
{
};
struct Base
{
//function with same name as struct above,
//no error if this is not templated
template <int N>
void f(){}
};
//template function that will be instantiated with T=Base.
//no error if this is not templated
template <class T>
void g(T t)
{
//I understand why template keyword is needed here,
//but not why T:: is needed
t.T::template f<0>();
//t.template f<0>(); gives error.
}
有問題的錯誤的是(從G ++ - 4.7)
error: type/value mismatch at argument 1 in template parameter list for ‘template<class T> struct f’
error: expected a type, got ‘0’
看來編譯器在解析(不帶範圍解析運算符)註釋爲試圖實例化struct f<0>
類型的對象。我不知道它爲什麼這樣做,我認爲它應該能夠從t.template
看到我試圖訪問成員函數模板。
我想明白是怎麼回事就在這裏,爲什麼需要在這種情況下T::
,除了安撫編譯器?
似乎有MSVC和鐺下沒問題,所以它似乎A G ++ - 具體問題。
嘗試調用'克(基地());'在'主()'。它會失敗,因爲它無法找到'基地:: F()' – SirGuy
剛剛更新使用T.T ::模板˚F<0>();嘗試一次。 – Arun
這正是我所說的。問題是爲什麼需要'T ::'? – SirGuy