typename in class templates - can the type be recognized inside a class?

To add comments, please log in or register
Amir Yacoby
1262
Amir Yacoby  

When using class templates, is it possible to know in the class which typename is the actual typename used?

For instance:

<typename T>
class ArraySort {.. is it possible  to know here what is T? }

The reason for asking, is for using the Save() or Load() methods, they have different FileWrite/FileRead functions for different types.

whroeder1
17927
whroeder1  
Amir Yacoby: is it possible  to know here what is T?

In C++ you would create partial template specializations to create different versions. MTx doesn't have that. So no, you don't know what T is.

Instead create specific overloaded functions, e.g. write(int i) write(double d) write(structM m) etc. Or assume that T can write itself.

Amir Yacoby
1262
Amir Yacoby  
whroeder1:

In C++ you would create partial template specializations to create different versions. MTx doesn't have that. So no, you don't know what T is.

Instead create specific overloaded functions, e.g. write(int i) write(double d) write(structM m) etc. Or assume that T can write itself.

Thanks.
Assuming that T can write itself, it's valid in objects. For primitive types (int, double..) there's a need to find other options.

The first idea, using specific overloaded functions, seems right for that. If T is primitive, and you have this set of overloaded funtions, you can pass a T to one of them - it should work, right? This is what you mean? (sorry, not able to try it myself soon).

Thank you, that was a useful answer 
Alain Verleyen
36610
Alain Verleyen  
Amir Yacoby:

When using class templates, is it possible to know in the class which typename is the actual typename used?

For instance:

<typename T>
class ArraySort {.. is it possible  to know here what is T? }

The reason for asking, is for using the Save() or Load() methods, they have different FileWrite/FileRead functions for different types.

      if(typename(T)=="int")
      ...


Alain Verleyen
36610
Alain Verleyen  
Amir Yacoby:
Thanks.
Assuming that T can write itself, it's valid in objects. For primitive types (int, double..) there's a need to find other options.

The first idea, using specific overloaded functions, seems right for that. If T is primitive, and you have this set of overloaded funtions, you can pass a T to one of them - it should work, right? This is what you mean? (sorry, not able to try it myself soon).

Thank you, that was a useful answer 
You can have a generic template AND specialized versions.
Amir Yacoby
1262
Amir Yacoby  
Alain Verleyen:
You can have a generic template AND specialized versions.

Yes, like whroeder1 suggested, inside the class template you can do spealizations.

<typename T>
class Sort
{
private:
bool Save(const int handle,int type);
bool Save(const int handle,double type);
bool Save(const int handle,string type);
public:
bool Save(const int handle);
};

bool Sort::Save(const int handle)
{
T type;
return Save(handle,type);
}
Alain Verleyen
36610
Alain Verleyen  
Amir Yacoby:

Yes, like whroeder1 suggested, inside the class template you can do spealizations.

No it's not what I mean. I was talking about function template.
Amir Yacoby
1262
Amir Yacoby  
Alain Verleyen:
No it's not what I mean. I was talking about function template.

I see. 
But I don't want to give up on the class template.

Alain Verleyen
36610
Alain Verleyen  
Amir Yacoby:

I see. 
But I don't want to give up on the class template.

Not needed, isn't this what you need ?

Amir Yacoby
1262
Amir Yacoby  
Alain Verleyen:

Not needed, isn't this what you need ?

Yes!!

I remembered something like this somewhere but couldn't find it in docs.
Now I looked again and don't understand how I missed it, it's right there (:

thank you

To add comments, please log in or register