|
-
November 14th, 2008, 05:25 PM
#1
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?
-
November 14th, 2008, 05:27 PM
#2
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.
-
November 14th, 2008, 05:40 PM
#3
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.
-
November 14th, 2008, 07:24 PM
#4
Re: How to check if template is a pointer?
 Originally Posted by Red Squirrel
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
-
November 14th, 2008, 07:38 PM
#5
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
-
November 14th, 2008, 07:57 PM
#6
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.
-
November 14th, 2008, 08:33 PM
#7
Re: How to check if template is a pointer?
 Originally Posted by Red Squirrel
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
-
November 14th, 2008, 09:32 PM
#8
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.
-
January 30th, 2010, 02:05 PM
#9
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.
-
January 30th, 2010, 07:19 PM
#10
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,
 Originally Posted by pkhalladin
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.
-
January 31st, 2010, 10:01 AM
#11
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
-
January 31st, 2010, 10:15 AM
#12
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)
-
January 31st, 2010, 01:12 PM
#13
Re: How to check if template is a pointer?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|