How to check is T a pointer in template<class T>?

Hello;

I just want to check type T for a class or in a function with a type template.

For example
template <class T>
void myFunc ( T x )
{...}

How can I do it? Is it a pointer to anything, or a double, or an int...?
What?
I think it's clear enough.
Well I'm not sure I understand you.
What are you checking for?
(And frankly, it's not my job to understand your question, but your job to make me understand. The onus is on you, the question asker, to make sure that your question is clear, and not on me to figure it out.)
Last edited on
I just couldn't find any way to explain more.

myFunc() is a template function so you run it like myFunc<int>();

I just want to know it run with/for which type.

Some pseudo code

1
2
3
4
5
6
7
8
9
void myfunc ()
{
  if T is double
    ...
  else if T is int
    ...
  else
    ...
}



Because think about class

1
2
3
4
5
6
7
8
9
10
11
12
13
template <class T>
class A
{
public:
T varA;
...
...
~A() //destructor
  {
    if T is pointer
      delete varA;
  }
};


It is useful if we could check the type for deallocating memory.
OK. (See, you could explain it more and now it makes excellent sense.)
In your class case you want to, ideally, avoid situations like that, where you need to delete *conditionally* (chances are that wouldn't happen anyway) but the technique for what you are trying to do is, in some ways, related to runtime type ID (RTTI). RTTI is for determining the dynamic type of a base-class object at runtime. However, I am not sure how to resolve your problem.
The solution is to write a template specialization for the worrisome types in question (in this case, pointers of some kind). Unfortunately you may end up writing tons of specializations. Here's the article on templates for you to analyze: http://www.cplusplus.com/doc/tutorial/templates/
In the concept you provide I would generally not find any case in which that would happen because all the dynamic allocation happens in the class's own construction and methods no? You cannot choose to pass a dynamically allocated "type" to a class, I believe. (Unless you passed an object containing DMA'd stuff, in which case that class's destructor should handle the deallocation.)
if T is pointer
delete varA;
How do you know the pointer was dynamically allocated? How do you know it was allocated by new? What if it was allocated by new[] or malloc()?
Trust me. You don't want to do this.
I second helios; be very clear about your dynamic allocation if you are going to use it because there is no way to tell if it was dynamically allocated.
It doesnt' work like that. What you do is define a template with default behaviour, then define alternative versions depending on the properties of the types.

You wouldn't expect to see an if ... else if ... type structure, but a set of different versions.

If you can provide a more concrete example of your problem, I can give a more specific answer. In the mean time, there's an example here: http://www.cplusplus.com/forum/general/13429/
That's a really nice and smart solution.

Code is a little complex but i'm examining it.

I also didn't expect to see an if, else if statement. Just wanted to point out what I'm searching for with pseudo code.

Thanks.
Topic archived. No new replies allowed.