Valkea Valkea - 2 years ago 54
C++ Question

How to get T* from T, T& or T* template parameter

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

Consider the following class:

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


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

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


  • 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.



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

Answer Source

How about using std::remove_reference?

#include <type_traits>

template <class C>
class Foo
{
    public:
        Foo(){};
        ~Foo(){};
        std::remove_reference<C>::type *ptr;
};
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download