I have to following class:
template<typename T>
class SafeCallback
{
public:
typedef std::function<T> FunctionType;
SafeCallback(std::shared_ptr<bool> is_valid, FunctionType callback)
: is_valid_(is_valid), callback_(callback)
{
}
template <class ...Arg>
void operator()(Arg&&... parameters)
{
if ((*is_valid_) == true)
{
callback_(std::forward<Arg>(parameters)...);
}
}
private:
std::shared_ptr<bool> is_valid_;
FunctionType callback_;
};
To construct such an object with a lambda as the callback I can do something like this:
SafeCallback<void(int)>(guard, [] (int value) { /* Do sthing */ });
But I would imagine that there should be a way for C++ to infer the type arguments of SafeCallback using a factory method. If I create such a method:
template<typename T>
SafeCallback<T> makeSafe(std::shared_ptr<bool> is_valid, std::function<T> callback)
{
return SafeCallback<T>(is_valid, callback);
}
But this doesnt work with lambdas, only if I pass a std::function. Any ideas?
Aucun commentaire:
Enregistrer un commentaire