gpt4 book ai didi

c++ - Singleton模板特化编译怪异错误

转载 作者:搜寻专家 更新时间:2023-10-31 01:54:18 26 4
gpt4 key购买 nike

我对使用类和函数作为参数的模板有疑问。问题是,当只声明和定义 AAInterceptor 时,它工作正常。当我添加 BBInterceptor 时,出现了一个看起来很奇怪的编译器错误。

这是代码

template< class T>
class Singleton
{
protected:
static T* ms_Singleton;
Singleton()
{
if(!ms_Singleton)ms_Singleton = new T;
}
public:
static T& getSingleton( void ){assert( 0); return *ms_Singleton; }
};

struct InterceptorData
{
unsigned int flag;

InterceptorData():flag(0){}
};

template <class C, void (C::*TMethod)(const InterceptorData*)>
class InterceptorManager : public Singleton< InterceptorManager<C,TMethod> >
{

};

class AClass
{
public:
virtual void Amethod(const InterceptorData* p_data = 0){};
};

class AAInterceptor : public InterceptorManager<AClass, &AClass::Amethod>
{
public:
static AAInterceptor& getSingleton(void)
{
if (!ms_Singleton) new AAInterceptor();
assert( ms_Singleton );
return ( *(static_cast< AAInterceptor*>(ms_Singleton)) );
}
};

class BClass
{
public:
virtual void Bmethod(const InterceptorData* p_data = 0){};
};

class BBInterceptor : public InterceptorManager<BClass, &BClass::Bmethod>
{
public:
static BBInterceptor& getSingleton(void)
{
if (!ms_Singleton) new BBInterceptor();
assert( ms_Singleton );
return ( *(static_cast< BBInterceptor*>(ms_Singleton)) ); //Here is the error of compilation
}
};

int main(void)
{
AAInterceptor a;
BBInterceptor b;
return 0;
}

它可以用 GCC (http://codepad.org/Bi6zbsmq) 编译,但不能用 MSVC2008。

这是 Visual Studio 中的错误:

error:  " error C2440: 'static_cast' : cannot convert from 'InterceptorManager<C,TMethod> *' to 'BBInterceptor *' " 58
with
1> [
1> C=BClass,
1> TMethod=void AClass::`vcall'{0}'(const InterceptorData *)
1> ]
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

似乎编译器将不同的模板混合在一起,不是吗?

提前感谢您的帮助

最佳答案

这段代码不会导致编译错误,只是将虚调用包装到非虚函数中并获取它的地址。

template<typename T>
class Singleton
{
protected:
static T* ms_Singleton;
Singleton()
{
if(!ms_Singleton)ms_Singleton = new T;
}
public:
static T& getSingleton( void ){assert(0); return *ms_Singleton; }
};

template<typename T>
T* Singleton<typename T>::ms_Singleton = 0;

struct InterceptorData
{
unsigned int flag;
InterceptorData():flag(0){}
};

template <typename C, void (C::*TMethod)(const InterceptorData*)>
class InterceptorManager : public Singleton< InterceptorManager<C,TMethod> >
{
};

class AClass
{
public:
void Amethod(const InterceptorData* p_data = 0){AmethodImpl(p_data);};
virtual void AmethodImpl(const InterceptorData* p_data = 0){};
};

class AAInterceptor : public InterceptorManager<AClass, &AClass::Amethod>
{
public:
static AAInterceptor& getSingleton(void)
{
if (!ms_Singleton) new AAInterceptor();
assert( ms_Singleton );
return ( *(static_cast< AAInterceptor*>(ms_Singleton)) );
}
};

class BClass
{
public:
void Bmethod(const InterceptorData* p_data = 0){BmethodImpl(p_data);};
virtual void BmethodImpl(const InterceptorData* p_data = 0){};
};

class BBInterceptor : public InterceptorManager<BClass, &BClass::Bmethod>
{
public:
static BBInterceptor& getSingleton(void)
{
if (!ms_Singleton) new BBInterceptor();
assert( ms_Singleton );
return ( *(static_cast< BBInterceptor* >(ms_Singleton)) );
}
};

int main(void)
{
AAInterceptor a;
BBInterceptor b;
return 0;
}

关于c++ - Singleton模板特化编译怪异错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9887033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com