第一次我在同一時間使用模板和指向成員函數的指針,並且偶然發現了以下問題。使用模板函數指針聲明模板函數
我聲明瞭一個typedef的結構代理,因爲模板和typedefs不能一起工作(我知道這應該可以在C++ 11中,MSVC++不會接受它)。現在我想聲明一個使用模板類型代理的模板化(!)函數。這是編譯時導致錯誤的原因。請看下面的(簡化)代碼,我添加了一些例子來澄清問題。
我正在使用標準VC++ 2010(無CLR/CLI)。
template<typename T>
struct Proxy
{
typedef bool (CClass::*MethodPtr)(const T* const objectX);
}
class CClass
{
// warning: dependent name is not a type
// error:: Method can not be a template definition
template<typename T>
bool Method(Proxy<T>::MethodPtr);
// this works, but i want to specify T instead of int of course
template<typename t>
bool method(Proxy<int>::MethodPtr);
// this is what i use
template<typename T>
bool Method(bool (CClass::*MethodPtr)(const T* const objectX));
}
template<typename T>
bool CClass::Method(bool (CClass::*MethodPtr)(const T* const objectX))
{
// next line compiles without problems
Proxy<T>::MethodPtr ptr2;
}
您需要在函數原型中使用'typename'。 – moshbear