在C ++中获取给定指向它的指针的类型名的类型名

Xoc*_*zin 0 c++ templates pointers typename c++11

考虑以下代码:

class c {
  //...
};

template <typename T>
void f(T k)
{
    auto item = new T;
    //...
}
Run Code Online (Sandbox Code Playgroud)

我们声明一个类c和一个f创建新对象类型的模板函数T。

我想更改此函数,f以便template参数可以是指针类型,它将按以下方式使用:

auto ptr = new c;
f<c*>(ptr);
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试创建一个新项目时出现了问题auto item = new T;,因为现在T是指向的指针的类型名c。

我知道T它将永远是指向某个东西的指针,我怎样才能得到所指向的类型名T?我想做类似的事情:

template <typename T>
void f(T k)
{
    // If T = int* -> Q = int
    typename ??????? Q;    // <<<<<<<<
    auto item = new Q;
    //...
}
Run Code Online (Sandbox Code Playgroud)

Win*_*ute 5

与std::remove_pointer:

#include <type_traits>

using Q = typename std::remove_pointer<T>::type;
Run Code Online (Sandbox Code Playgroud)