CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jul 2007
    Posts
    609

    How to check if template is a pointer?

    I have a class that is wrapper to vector that just makes it easier to do common tasks as well as has other features. it uses templates so I can make various types. How can I determine if a type is a pointer or not? I want to make a function to free the memory of all the elements but I only want it to work on pointers as it would probably crash if I did it to a non pointer.

    So say I got this:

    template <class T>

    How do I check if T is a pointer type?
    http://www.uovalor.com :: Free UO Server

  2. #2
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: How to check if template is a pointer?

    Just do *T somewhere in the code. This will then give a compile time error to people who use it without a pointer.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to check if template is a pointer?

    Bad idea to go trying to free things you don't fully understand. In the event the memory in question needs freeing, just wrap it in a shared_ptr or something similar prior to using your template.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to check if template is a pointer?

    Quote Originally Posted by Red Squirrel View Post
    I want to make a function to free the memory of all the elements but I only want it to work on pointers as it would probably crash if I did it to a non pointer.
    Just because it's a pointer doesn't mean it points to dynamically allocated memory.
    Code:
    void func(int *p)
    {
       delete p;
    }
    
    int main()
    {
        int x;
        func( &x );
    }
    I hope the code above illustrates my point. The x was not dynamically allocated, so issuing a "delete" will more than likely crash.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: How to check if template is a pointer?

    I agree with PAul and Lindley, the ONLY time I would implement a collection with a "delete" anywhere in it, is if the collection was also THE source of the objects via "new".
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  6. #6
    Join Date
    Jul 2007
    Posts
    609

    Re: How to check if template is a pointer?

    The way I often use these is I just use new directly when adding the objects in them, so to have such function would be useful. But yeah if I call it where I should not I realize it would be a bad idea.
    http://www.uovalor.com :: Free UO Server

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: How to check if template is a pointer?

    Quote Originally Posted by Red Squirrel View Post
    The way I often use these is I just use new directly when adding the objects in them, so to have such function would be useful. But yeah if I call it where I should not I realize it would be a bad idea.
    I am a firm believer in "Safe Designs", The pholosphy is known by a number of terms, including "Defensive Programming".

    The basic premise is to not only make things "Easy to Use Correctly", but to embrace "Diffficult to Use Improperly".

    Protecting myself against those "Moments of Incredible Stupidity" has saved my butt, more times than I care to count...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: How to check if template is a pointer?

    I agree with TheCPUWizard.

    Design by contract or programming by contract is another secure coding method.
    Last edited by Peter_APIIT; November 14th, 2008 at 10:05 PM.
    Thanks for your help.

  9. #9
    Join Date
    Jan 2010
    Posts
    1

    Post Re: How to check if template is a pointer?

    You may create template function like this:

    template <class T>
    bool IsPointerType()
    {
    return strchr(typeid(T).name(), '*') != 0;
    }

    Then check if some type is pointer or not:

    cout << IsPointerType<int>() << endl;
    cout << IsPointerType<int*>() << endl;

    In second case the answer is "1" (in first "0"). In your template class checking is similiar but
    I suggest to do this in constructor and then set some flag:

    SomeClass<T>::SomeClass()
    {
    // this is member flag variable of type bool
    _isPointerType = IsPointerType<T>();
    // rest of constructor stuff
    }

    Then you may query this flag to know if type is pointer or not.

  10. #10
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: How to check if template is a pointer?

    The implementation of typeid::name() varys among the compilers.
    The only consistent result guaranteed by the language is the result of evaluating an equality operation. e.g,
    Code:
     typeid(X) == typeid(Y);
    Unless you're the only one using the code,
    Quote Originally Posted by pkhalladin View Post
    You may create template function like this:
    Code:
    template <class T>
    bool IsPointerType()
    {
        return strchr(typeid(T).name(), '*') != 0;
    }
    this is not the recommended way.

    Edit:
    Ah, this is a zombie thread.

    I see the name I haven't seen in a while, TheCPUWizard.
    Last edited by potatoCode; January 30th, 2010 at 07:25 PM.

  11. #11
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: How to check if template is a pointer?

    Since it wasn't mentioned above, there is an infallible way to check if a type is a pointer to a complete object (that is, not void*).
    Code:
    template <class T>
    void foo(T p)
    {
        0[p]; // check that T is a ponter
    }
    
    int main()
    {
        int x;
        int* pInt = &x;
        void* pVoid = &x;
    
        foo(pInt); // ok
        foo(pVoid); // error: expression must be a pointer to a complete object
        foo(x); // error: expression must have pointer-to-object type
    }
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  12. #12
    Join Date
    Jul 2009
    Posts
    37

    Re: How to check if template is a pointer?

    Seems like you are messing up readers' mind instead, for less concentration on the truth exposed out there
    What reason do you have to find the type of a pointer ?
    Sig-na-tju-(r)

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to check if template is a pointer?

    Holy gravedig, Batman!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured