Boost:取消引用模板参数,如果它是指针

Ale*_*x B 7 c++ templates

如果模板参数是指针(或智能指针),我可以使用什么来取消引用模板参数,如果不是,则将其保留为原样?

template<class T> void subf(const T& item)
{
    item.foo();
}

template<class T> void f(const T& item)
{
    subf(magic_dereference_function(item));
}
Run Code Online (Sandbox Code Playgroud)

Boost中的任何东西都是一种选择.

Pet*_*der 12

template <typename T>
T& maybe_deref(T& x) { return x; }

template <typename T>
T& maybe_deref(T* x) { return *x; }
Run Code Online (Sandbox Code Playgroud)

您必须单独为智能指针添加重载.没有办法检测一个类是否是"智能指针".你可以检测到它的存在operator->,但这并不意味着它是一个智能指针.