如何为指针类创建静态模板函数?

时间:2013-06-02 13:19:03

标签: c++ class templates static

我的模板类A包含一个调用模板类的静态函数的函数:

template <typename T>
void A<T>::fun() {
    T obj = T::create();
    ....
}

如果我想在T = B *时使用此代码,我该如何修改? 我知道我做不到(* T):: create(),但从概念上讲,这就是我想要的。

1 个答案:

答案 0 :(得分:5)

您可以使用std::remove_pointer类型特征:

#include <type_traits>

template <typename T>
void A<T>::fun() {
    T obj = std::remove_pointer<T>::type::create();
//          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    // ...
}

std::remove_pointer<U*>::typestd::remove_pointer<U>::type都提供U

相关问题