第一次同时使用模板和指向成员函数的指针时,我遇到了以下问题。
我为一个类型定义声明了一个结构代理,因为模板和类型定义不能一起工作(我知道这在C++11中应该是可能的,但是MSVC++不接受它)。现在我想声明一个模板(!)函数,该函数使用带有模板类型的代理。这就是导致编译时出错的原因。请看下面的(简化的)代码,我添加了一些示例来澄清问题。
我使用的是标准的VC++ 2010 (无命令行/命令行界面)。
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;
}发布于 2011-11-17 00:49:39
您需要使用typename来指示依赖名称是类型的位置;特别是,Method的第一个声明应该是:
template<typename T>
bool Method( typename Proxy<T>::MethodPtr );您所说的编译没有问题(但只是因为VC++有一个接受格式错误代码的“扩展”)的代码行应该是:
// next line compiles without problems
typename Proxy<T>::MethodPtr ptr2;您还缺少类定义后的分号,以及Proxy定义之前的CClass正向声明。
发布于 2011-11-17 00:47:29
// warning: dependent name is not a type
// error:: Method can not be a template definition
template<typename T>
bool Method( Proxy<T>::MethodPtr );MethodPtr依赖于模板参数T,您应该使用typename。
// next line compiles without problems
Proxy<T>::MethodPtr ptr2;您也应该在那里使用typename,但不确定它为什么会编译。
https://stackoverflow.com/questions/8155366
复制相似问题