Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   knowing if a template parameter is a pointer (http://www.velocityreviews.com/forums/t458271-knowing-if-a-template-parameter-is-a-pointer.html)

Vincent RICHOMME 11-08-2006 10:49 PM

knowing if a template parameter is a pointer
 
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;
};


Victor Bazarov 11-08-2006 10:52 PM

Re: knowing if a template parameter is a pointer
 
Vincent RICHOMME wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



Noah Roberts 11-08-2006 11:03 PM

Re: knowing if a template parameter is a pointer
 

Vincent RICHOMME wrote:
> 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;
};



All times are GMT. The time now is 02:55 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.