我知道如何写一些东西,但我确信有一种标准的方式来传递类似的东西func<TheType*>()
并使用模板魔术来提取 TheType 以在代码中使用(可能是 TheType::SomeStaticCall)。
传入 ptr 时获取该类型的标准方法/函数是什么?
我认为您想从函数的类型参数中删除指针。如果是这样,那么您可以这样做,
template<typename T>
void func()
{
typename remove_pointer<T>::type type;
//you can use `type` which is free from pointer-ness
//if T = int*, then type = int
//if T = int****, then type = int
//if T = vector<int>, then type = vector<int>
//if T = vector<int>*, then type = vector<int>
//if T = vector<int>**, then type = vector<int>
//that is, type is always free from pointer-ness
}
where remove_pointer
定义为:
template<typename T>
struct remove_pointer
{
typedef T type;
};
template<typename T>
struct remove_pointer<T*>
{
typedef typename remove_pointer<T>::type type;
};
在 C++0x 中,remove_pointer
定义于<type_traits>
头文件。但在C++03中,你必须自己定义它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)