Ale*_*lex 1 c++ templates function-pointers visual-c++
我第一次同时使用模板和指向成员函数的指针,我偶然发现了以下问题.
我为typedef声明了一个struct Proxy,因为templates和typedef不能一起工作(我知道这应该可以在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;
}
Run Code Online (Sandbox Code Playgroud)
您需要使用它typename来指示依赖名称的类型; 特别是,第一个声明Method应该是:
template<typename T>
bool Method( typename Proxy<T>::MethodPtr );
Run Code Online (Sandbox Code Playgroud)
你说的编译没有问题的行(但只是因为VC++有一个"扩展"接受格式错误的代码)应该是:
// next line compiles without problems
typename Proxy<T>::MethodPtr ptr2;
Run Code Online (Sandbox Code Playgroud)
在类定义之后,您还缺少分号,并且CClass在定义之前还有前向声明Proxy.
| 归档时间: |
|
| 查看次数: |
628 次 |
| 最近记录: |