Template: how to detect type
-
I wrote a template class which can accept various data types (I had different kind of data to handle) All the data type I used with this template-class, are structures with common parameters. Now I need to write a common code to all classes (I was thinking of a function with his own class) to use these "common" parameters. The solutions I thought are: 1) Passing a void* pointer to the function and select the appropriate code for the datatype: void Function( void *pointer_to_custom_data, ENUM_DATA_TYPE) { if(ENUM_DATA_TYPE == DATA_TYPE1) // specific code for type1.. else if (ENUM_DATA_TYPE == DATA_TYPE2) // specific code for type2.. ... } 2) Overloading in some way this function (don't know how though) so it recognize the type used and uses the specific code Any better idea or suggestion on how to implement these?
---
-
I wrote a template class which can accept various data types (I had different kind of data to handle) All the data type I used with this template-class, are structures with common parameters. Now I need to write a common code to all classes (I was thinking of a function with his own class) to use these "common" parameters. The solutions I thought are: 1) Passing a void* pointer to the function and select the appropriate code for the datatype: void Function( void *pointer_to_custom_data, ENUM_DATA_TYPE) { if(ENUM_DATA_TYPE == DATA_TYPE1) // specific code for type1.. else if (ENUM_DATA_TYPE == DATA_TYPE2) // specific code for type2.. ... } 2) Overloading in some way this function (don't know how though) so it recognize the type used and uses the specific code Any better idea or suggestion on how to implement these?
---
I'm not sure i completely understand your problem, but the first thought that comes to my mind is: why don't you have a common base class for your structures that contains the common "parameters" you are talking about, then you can make a method that takes a pointer/reference to this base class and does with the parameters what it should. Or do you mean that you have to do different things depending on the type of struct? Then use a virtual function declared in the base class and implement it according to your needs in all the subclasses. Am i misunderstanding you?
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <
-
I wrote a template class which can accept various data types (I had different kind of data to handle) All the data type I used with this template-class, are structures with common parameters. Now I need to write a common code to all classes (I was thinking of a function with his own class) to use these "common" parameters. The solutions I thought are: 1) Passing a void* pointer to the function and select the appropriate code for the datatype: void Function( void *pointer_to_custom_data, ENUM_DATA_TYPE) { if(ENUM_DATA_TYPE == DATA_TYPE1) // specific code for type1.. else if (ENUM_DATA_TYPE == DATA_TYPE2) // specific code for type2.. ... } 2) Overloading in some way this function (don't know how though) so it recognize the type used and uses the specific code Any better idea or suggestion on how to implement these?
---
-
I wrote a template class which can accept various data types (I had different kind of data to handle) All the data type I used with this template-class, are structures with common parameters. Now I need to write a common code to all classes (I was thinking of a function with his own class) to use these "common" parameters. The solutions I thought are: 1) Passing a void* pointer to the function and select the appropriate code for the datatype: void Function( void *pointer_to_custom_data, ENUM_DATA_TYPE) { if(ENUM_DATA_TYPE == DATA_TYPE1) // specific code for type1.. else if (ENUM_DATA_TYPE == DATA_TYPE2) // specific code for type2.. ... } 2) Overloading in some way this function (don't know how though) so it recognize the type used and uses the specific code Any better idea or suggestion on how to implement these?
---
This looks likes bad design to me. Template class should not be explicitly aware of the types it can be used with. It can define the requirements in the types but that is all. In your case it looks like better option is to use inheritance instead of template class. Unless of course you have presented a simplified picture. I will suggest you inherit the structures from a common base class. This base class will contain common parameters and a pure virtual function. Inherited structures will override this function providing the type specific functionality. -Saurabh
-
I wrote a template class which can accept various data types (I had different kind of data to handle) All the data type I used with this template-class, are structures with common parameters. Now I need to write a common code to all classes (I was thinking of a function with his own class) to use these "common" parameters. The solutions I thought are: 1) Passing a void* pointer to the function and select the appropriate code for the datatype: void Function( void *pointer_to_custom_data, ENUM_DATA_TYPE) { if(ENUM_DATA_TYPE == DATA_TYPE1) // specific code for type1.. else if (ENUM_DATA_TYPE == DATA_TYPE2) // specific code for type2.. ... } 2) Overloading in some way this function (don't know how though) so it recognize the type used and uses the specific code Any better idea or suggestion on how to implement these?
---
If you want type specific code, you do not write a template class. In some cases, for template classes you need a special implementation for a certain type. In these cases you can do template specialization. Assume you have a template class called Math -
template
class Math
{
};You may need special implementation for some data type like complex. In this case you can specialize the template class for the complex data type.
template<>
class Math
{
};«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
I wrote a template class which can accept various data types (I had different kind of data to handle) All the data type I used with this template-class, are structures with common parameters. Now I need to write a common code to all classes (I was thinking of a function with his own class) to use these "common" parameters. The solutions I thought are: 1) Passing a void* pointer to the function and select the appropriate code for the datatype: void Function( void *pointer_to_custom_data, ENUM_DATA_TYPE) { if(ENUM_DATA_TYPE == DATA_TYPE1) // specific code for type1.. else if (ENUM_DATA_TYPE == DATA_TYPE2) // specific code for type2.. ... } 2) Overloading in some way this function (don't know how though) so it recognize the type used and uses the specific code Any better idea or suggestion on how to implement these?
---
Function specialisation. Specialization if you're American. Use the standard template case for the common code, specialisations for code specific to types. Like this:
#include <iostream>
template<class _Type>
void Fn(_Type const&)
{
std::cout << "Generic..." << std::endl;
}template<>
void Fn<int>(int const&)
{
std::cout << "int specialisation" << std::endl;
}int main()
{
Fn('a');
Fn(10);
}The output you get is:
Generic...
int specialisationJava, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!
-
Function specialisation. Specialization if you're American. Use the standard template case for the common code, specialisations for code specific to types. Like this:
#include <iostream>
template<class _Type>
void Fn(_Type const&)
{
std::cout << "Generic..." << std::endl;
}template<>
void Fn<int>(int const&)
{
std::cout << "int specialisation" << std::endl;
}int main()
{
Fn('a');
Fn(10);
}The output you get is:
Generic...
int specialisationJava, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!
I'm not dead, I was just looking at your suggestions and see whether they're appliable to my project. Thank you guys but I realized some things: 1) I'm an idiot 2) More than half of my project has to be rewritten. I can't specializate template class because datatype are too much. 3) Project is slow as hell, I screwed it up with template of templates and at runtime it has to calculate every damn address it uses I think I'm going to change a lot of things... thank you for your help!
---
-
I wrote a template class which can accept various data types (I had different kind of data to handle) All the data type I used with this template-class, are structures with common parameters. Now I need to write a common code to all classes (I was thinking of a function with his own class) to use these "common" parameters. The solutions I thought are: 1) Passing a void* pointer to the function and select the appropriate code for the datatype: void Function( void *pointer_to_custom_data, ENUM_DATA_TYPE) { if(ENUM_DATA_TYPE == DATA_TYPE1) // specific code for type1.. else if (ENUM_DATA_TYPE == DATA_TYPE2) // specific code for type2.. ... } 2) Overloading in some way this function (don't know how though) so it recognize the type used and uses the specific code Any better idea or suggestion on how to implement these?
---
Hi 4288! My spontaneous reaction was, why does he use templates and not polymorphism... but I wanted to see what others say. Perhaps it can help you in your design to have an abstract base class
CObject
and then derive classes from that base class with further specialisation. You would then only need one template for the base class (or if it is about storing data something likestd::vector<CObject*>
). In C++ virtual methods will be called virtual even via a base class pointer, quite handy. Hard to give better tips without seeing your architecture or requirements. Looks like Saurabh gave the same advice. Hope it helps... and happy refactoring! :) /MWebchat in Europe :java: Now with 26% more Twitter