I have the following smart_ptr
class.
template <typename T>
class smart_ptr
{
public:
// ... removed other member functions for simplicity
T* get() { return ptr; }
template <typename... Args>
decltype(T::template operator ()(Args()...)) operator ()(Args... args) const
{
return (*get()).operator ()(args...);
}
private:
T* ptr;
};
However, when I use the smart_ptr
class for type T without an operator ()
, it fails to compile (and rightly so).
What I wanted is to use std::enable_if
to enable the member function only when T has operator ()
. It got obscure and complex very fast when I got into doing it, as I was thinking of using SFINAE to detect if T has operator ()
and then combine it with std::enable_if
to get what I want. However, I got lost in creating SFINAE that is capable of detecting a variadic templated operator ()
(i.e. NOT a duplicate of Is it possible to write a template to check for a function's existence?).
Can anyone help? Or give another (perhaps simpler) solution?
p.s. It has to work on GCC 4.5.3.
EDIT: For the sake of completeness, I've answered my own question with a working solution for GCC 4.5.3.