Singleton模板专业化编译的奇怪错误

时间:2012-03-27 09:45:14

标签: c++ templates compiler-errors singleton functor

我使用类和函数作为参数的模板有问题。 问题是,当只声明和定义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

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

Thanx提前为您提供帮助

1 个答案:

答案 0 :(得分:3)

此代码不会导致编译器错误,只需将虚拟调用包装到非虚函数中并获取地址。

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;
}