Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Type selector please help!

  1. Sep 15, 2004 #1
    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 (Text):
    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){
                    std::ifstream infile(filename);

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

                    std::cerr<<"Readfile error\n";
    Thanks in advance
  2. jcsd
  3. Sep 15, 2004 #2
    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.
  4. Sep 16, 2004 #3


    User Avatar
    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:

    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.
  5. Sep 17, 2004 #4
    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.

    ---really sorry for my bad english and misunderstandings if any.
  6. Sep 18, 2004 #5
    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 (Text):

    // 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;
    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.
  7. Sep 19, 2004 #6
    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.
  8. Sep 19, 2004 #7
    What type problems?
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook