## 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;
};
try{
std::ifstream infile(filename);

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

catch(...){
exit(EXIT_FAILURE);
}
}
Regards,

Related Computing and Technology News on Phys.org
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.

dduardo
Staff Emeritus
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:

classnamebject_name {variable1 , classnamebject_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.

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.

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.

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.

What type problems?