knowing if a template parameter is a pointer

  • Thread starter Vincent RICHOMME
  • Start date
V

Vincent RICHOMME

Hi,

I would like to port a .NET list into standard c++
and in particular is there a way of knowing if the object is a pointer
or not ?

Ex :

template<typename T>
class List : public std::list<T>
{
public:


~List() { Clear(); }

inline void Add(T object)
{
// IS IT POSSIBLE TO KNOW if object is a pointer ???????????
if ( ISAPONITER(object) )
bIsPtr = true;
else
bIsPtr = false;
push_back(object);
}

inline void Clear(bool bDeallocate = true)
{
if (bDeallocate && m_bIsPtr)
{
int count = this->size();
// ???? I SHOULD PARSE MY LIST AND DEALLOCATE
this->std::list<T>::clear();
}
}

inline int GetCount() { return size(); }

private:
bool m_bIsPtr;
};
 
V

Victor Bazarov

Vincent said:
I would like to port a .NET list into standard c++
and in particular is there a way of knowing if the object is a pointer
or not ?

What does it mean for it to be a pointer? Why do you have that
requirement? Try using -> with it, and if it's a pointer (or a class
that pretends to be a pointer), the code will successfully compile.
If the operator-> is not defined for that type, the compilation will
fail.

You can also specialise your template for all pointers.

So the solution depends on what you need to do.
Ex :
[..]

V
 
N

Noah Roberts

Vincent said:
Hi,

I would like to port a .NET list into standard c++
and in particular is there a way of knowing if the object is a pointer
or not ?

This already exists in boost::type_traits I am sure but...

template < typename T>
struct is_pointer
{
enum { value = false };
};

template < typename T >
struct is_pointer < T * >
{
enum { value = true };
}

then it becomes as simple as something like:

template < typename T >
class X
{
static bool const is_ptr = is_pointer<T>::value;
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top