OVH Cloud OVH Cloud

Template, spécification, et compilateur déficient...

2 réponses
Avatar
Mathieu Peyréga
Bonsoir,

soit le code suivant qui me permet de gérer des objets de type callback
qui permettent de traiter des évènements (interface utilisateur,
évènement réseau, ...)
Ce code est-il légal ?
En tout cas (sauf erreur de reproduction de ma part), il compile (et à
l'air de foncitonner) bien sous gcc 3.4.2.
La version en ligne de Comeau ne dit rien de spécial non plus...

En revanche, sous Visual Studio 6 sp6, la spécialisation partielle pose
problème et je cherche une solution pour contourner le problème, avez
vous quelque chose à me proposer ?

Cordialement,

Mathieu Peyréga
--
http://matioupi.free.fr/

template <class ArgType>
class CallbackBase;

class Callback
{
public:
virtual inline ~Callback(void) = 0;

template <class ArgType>
inline void operator()(ArgType const& arg)
{
(*dynamic_cast<CallbackBase<ArgType>* >(this))(arg);
}

void operator()(void);

}; // class Callback

inline Callback::~Callback(void)
{}

template <class ArgType>
class CallbackBase : public Callback
{
public:
virtual inline ~CallbackBase(void) {}

virtual void operator()(ArgType const& arg) = 0;

}; // class CallbackBase<ArgType>

template <>
class CallbackBase<void> : public Callback
{
public:
virtual inline ~CallbackBase(void) {}

virtual void operator()(void) = 0;

}; // class CallbackBase<void> (spécialisation)

inline void Callback::operator()(void)
{
(*dynamic_cast<CallbackBase<void>* >(this))();
}

template <class OwnerType, class ArgType>
class GenericCallback : public CallbackBase<ArgType>
{
public:
inline GenericCallback(OwnerType* pObj, void
(OwnerType::*pMethod)(ArgType const&));
inline virtual ~GenericCallback(void) {}

inline void operator()(ArgType const& arg);

protected:
OwnerType *m_pObj;
void (OwnerType::*m_pMethod)(ArgType const& arg);

}; // class GenericCallback<OwnerClass,ArgType>

template <class OwnerType, class ArgType>
inline GenericCallback<OwnerType,ArgType>::GenericCallback(OwnerType*
pObj, void (OwnerType::*pMethod)(ArgType const&))
:m_pObj(pObj), m_pMethod(pMethod)
{}

template <class OwnerType, class ArgType>
inline void GenericCallback<OwnerType,ArgType>::operator()(ArgType
const& arg)
{
(m_pObj->*m_pMethod)(arg);
}

template <class OwnerType>
class GenericCallback<OwnerType,void> : public CallbackBase<void>
{
public:
inline GenericCallback(OwnerType* pObj, void (OwnerType::*pMethod)(void));
inline virtual ~GenericCallback(void) {}

inline void operator()(void);

protected:
OwnerType *m_pObj;
void (OwnerType::*m_pMethod)(void);

}; // class GenericCallback<OwnerClass,void>

template <class OwnerType>
inline GenericCallback<OwnerType,void>::GenericCallback(OwnerType* pObj,
void (OwnerType::*pMethod)(void))
:m_pObj(pObj), m_pMethod(pMethod)
{}

template <class OwnerType>
inline void GenericCallback<OwnerType,void>::operator()(void)
{
(m_pObj->*m_pMethod)();
}

class OwnerA
{
public:
inline void do_it(void) {}
inline void do_it2(OwnerA const& obj) {}
};

int main(int argc, char* argv[])
{
OwnerA* pA = new OwnerA;

Callback* pCb1 = new GenericCallback<OwnerA,void>(pA,&OwnerA::do_it);
Callback* pCb2 = new GenericCallback<OwnerA,OwnerA>(pA,&OwnerA::do_it2);

(*(pCb1))();
(*(pCb2))(*pA);

delete pCb2;
delete pCb1;
delete pA;

return 0;
}

2 réponses

Avatar
Twxs
Mathieu Peyréga wrote:


En revanche, sous Visual Studio 6 sp6, la spécialisation partielle pose
problème et je cherche une solution pour contourner le problème, avez
vous quelque chose à me proposer ?

passer a un autre Visual, il me semble que c'est gere a partir du 7.1

mais je n'ai jamais essayé
sinon, faut se debrouiller pour ne pas a avoir a en faire de partielles

Avatar
Loïc Joly
Mathieu Peyréga wrote:

En revanche, sous Visual Studio 6 sp6, la spécialisation partielle pose
problème et je cherche une solution pour contourner le problème, avez
vous quelque chose à me proposer ?


Ils doivent faire ça dans boost, regarde le define
BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION. Par exemple, dans
http://www.boost.org/boost/math/common_factor_ct.hpp

--
Loïc