Type selector please help!

  • Thread starter kronecker
  • Start date
  • #1
19
0

Main Question or Discussion Point

How can i tell compuler select the correct type to whether to be able to 'infile' into int or float vars declared as private member in a class. sorry for my bad english.
Code:
template<bool TypeFlag, typename iType, typename fType>
		struct Selector{
			typedef iType Result;
		};
		
		template<typename iType, typename fType>
		struct Selector<false, iType, fType>{
			typedef fType Result;
		}; 
		void ReadFile(const char* filename){
			try{
				std::ifstream infile(filename);

				//////////////What can I do here///////////

			 catch(...){
				std::cerr<<"Readfile error\n";
				exit(EXIT_FAILURE);
			}
		}
Thanks in advance
Regards,
 

Answers and Replies

  • #2
259
0
I'm not sure I understand you. Do you want to have the computer read something from an infile and then decide whether it is an int or a float? In that case, it's impossible, unless you impose additional requirements (eg. int variables should be between 0 and 1000). Suppose you write something to an output file using something like:

int data_1 = 10;
float data_2 = 20;
outfile << data_1 << data_2;


The problem is that no type information is saved - you have to keep track of what went in through some other method.
 
  • #3
dduardo
Staff Emeritus
1,898
3
Here's an idea: When you create the file print a character before the number to signify which type it is. For example

i123 d12.42

i = int
d = double

When you read back the file you'll know what type to store the variable in.

You can do the same thing with classes, for example:

classname:eek:bject_name {variable1 , classname:eek:bject_name {variable 1, variable 2} , variable 3}

When you convert variables or objects using this technique it is called serializing data. When you read back the data it is called unserializing.
 
  • #4
19
0
thank you, zefram_c and dduardo

i have a class that i would use templates, the types will be, say, typename T and typename U, and in this class, i have some other functions among which there will be some that are 100% the same as each other in implementation but differ in types, say X and Y, that will be passed later from code in main function. i am not checking types of variables, just looking for a way to 'switch' from type to type and ways to use that 'switcher' in actuality. i posted code above only as an example. so, do you know how to deal with this problem ? i think it is trivial to you. but i am quite a dumb guy and really busy these day, i can't think much, i need help.
thanks if you or someone could be a little bit more definitive about the problem i am having.

Regards,
---really sorry for my bad english and misunderstandings if any.
 
  • #5
34
1
kronecker, i don't understand what you want to do. What exactly is it that you want to "switch"? With your example above you could do something like:

Code:
// modify this at compile time, e.g. thru preprocessing based on platform
const bool a = true; 

template <typename T>
void f() {cout << typeid(T).name() << endl; }

int main()
{
   typedef Selector<a, int, float>::Result T;
   f<T>();
}
But I'm not sure where you could use something like this. Or even if I'm close to what you want.. BTW, the most important thing to notice in the code above is that "a" MUST be computable at compile time.
 
  • #6
19
0
i write simple classes for numerical computation. i was looking for ways to use that selector more effectively or maybe some other ways to handling type problems, my previous posts should already meantion, thank you very much for your help anyway.
 
  • #7
34
1
What type problems?
 

Related Threads for: Type selector please help!

Replies
1
Views
468
  • Last Post
Replies
3
Views
1K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
1
Views
5K
  • Last Post
Replies
5
Views
2K
  • Last Post
Replies
3
Views
1K
Replies
6
Views
3K
  • Last Post
Replies
4
Views
5K
Top