typename in class templates - can the type be recognized inside a class?
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.
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.
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
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") ...
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.
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); }
I see.
But I don't want to give up on the class template.
Not needed, isn't this what you need ?
Forum on trading, automated trading systems and testing trading strategies
typename in class templates - can the type be recognized inside a class?
Alain Verleyen, 2018.02.11 19:12
if(typename(T)=="int") ...
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
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.