Selecting Types for infile Variables

  • Thread starter Thread starter kronecker
  • Start date Start date
  • Tags Tags
    Variables
Click For Summary

Discussion Overview

The discussion revolves around how to manage variable types when reading from an input file in C++, specifically focusing on determining whether to store values as integers or floats in a class context. The conversation includes considerations of template programming and serialization techniques.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant questions how to have the compiler select the correct type for variables when reading from a file, suggesting the use of templates.
  • Another participant expresses skepticism about determining the type of data read from a file without additional constraints, noting that type information is not inherently saved in files.
  • A different suggestion involves prefixing data in the file with a character to indicate its type, proposing a serialization approach to distinguish between integers and floats.
  • One participant describes their use of templates in a class and seeks advice on switching between types without checking them explicitly, indicating a need for clarity on implementing such a "switch."
  • Another participant provides an example of using a compile-time constant to determine the type, but expresses uncertainty about its relevance to the original question.
  • A participant mentions writing simple classes for numerical computation and seeks more effective ways to handle type-related issues.
  • One participant questions what type problems are being referred to, indicating a lack of clarity in the discussion.

Areas of Agreement / Disagreement

Participants express differing views on how to handle type selection when reading from files, with no consensus reached on a definitive method or solution. The discussion remains unresolved regarding the best approach to the problem.

Contextual Notes

There are limitations in the discussion regarding assumptions about data formats and the necessity of additional information to determine types. The effectiveness of proposed solutions may depend on specific implementation details that are not fully explored.

Who May Find This Useful

This discussion may be of interest to programmers working with C++ who are dealing with file input/output and type management, particularly in the context of template programming and serialization techniques.

kronecker
Messages
19
Reaction score
0
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,
 
Computer science 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.
 
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:object_name {variable1 , classname:object_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?
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
5K
Replies
6
Views
2K
  • · Replies 75 ·
3
Replies
75
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
63
Views
6K
  • · Replies 21 ·
Replies
21
Views
5K
Replies
10
Views
2K