如何从T,T&或T *模板参数 [英] How to get T* from T, T& or T* template parameter

查看:92
本文介绍了如何从T,T&或T *模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种获取模板参数基类的方法.

I am trying to find a way to get the base class of a template parameter.

请考虑以下课程:

template <class C>
class Foo
{
    public:
        Foo(){};
        ~Foo(){};
        C* ptr;
};

如果 C 是参考(例如 Test& ),则 ptr 类型为C& *

if C is a reference (e.g. Test&) then ptr is type C&*

但是我需要获得一个基类的指针,无论C是引用,指针还是其他任何东西.

But I need to get a pointer the base class whether C is a reference, a pointer or anything else.

  • 如果 C Test& ,则 ptr 必须是 Test *
  • 如果 C Test * ,则 ptr 必须是 Test *
  • 如果 C Test ,则 ptr 需要进行 Test * ,依此类推.
  • if C is Test& then ptr need to be Test*,
  • if C is Test* then ptr needs to be Test*,
  • if C is Test then ptr needs to be Test*, etc.

反正有没有获取C的基础"类,以便我可以创建所需的指针?

Is there anyway to get the "base" class of C so I can create the pointer I need ?

推荐答案

如何使用 std::remove_reference ?

#include <type_traits>

template <class C>
class Foo
{
    public:
        Foo(){};
        ~Foo(){};
        std::remove_reference<C>::type *ptr;
};

这篇关于如何从T,T&amp;或T *模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆