knowing if a template parameter is a pointer

Discussion in 'C++' started by Vincent RICHOMME, Nov 8, 2006.

  1. 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;
    };
     
    Vincent RICHOMME, Nov 8, 2006
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Nov 8, 2006
    #2
    1. Advertising

  3. Vincent RICHOMME

    Noah Roberts Guest

    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;
    };
     
    Noah Roberts, Nov 8, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amadeus W. M.

    pointer to function template parameter

    Amadeus W. M., Sep 3, 2005, in forum: C++
    Replies:
    4
    Views:
    339
    Amadeus W. M.
    Sep 7, 2005
  2. Vijai Kalyan
    Replies:
    4
    Views:
    714
    Vijai Kalyan
    Nov 8, 2005
  3. Replies:
    3
    Views:
    278
  4. Stuart Redmann
    Replies:
    5
    Views:
    482
    Stuart Redmann
    Dec 14, 2007
  5. Isti
    Replies:
    2
    Views:
    730
Loading...

Share This Page