Having guard class on C++11 that is responsible to invoke some member function on scope exit:
template <class T, void (T::*op)()>
struct Guard
{
Guard(T*g):
_g(g){}
~Guard()
{
(_g->*op)();
}
T*_g;
};
Usage is very simple:
typedef Guard<Foo, &Foo::bar> FooGuard;
...
FooGuard g(&foo);
My question originated from existing shared_ptr<Foo>
. How to create specialization that keeps shared_ptr<T>
instead of T*
What I've already tried:
template <class T, void (T::*op)()>
struct Guard<std::shared_ptr<T>, op>
{
Guard(std::shared_ptr<T>& g):
_g(g){}
~Guard()
{
((*_g).*op)();
}
std::shared_ptr<T> _g;
};
But during compilation on G<std::shared_ptr<Foo>, &Foo::bar> g2(foo);
have foreseeable got:
error C2440: 'specialization' : cannot convert from 'overloaded-function' to 'void (__thiscall std::shared_ptr::* )(void)'
Aucun commentaire:
Enregistrer un commentaire