3

I need to be able to pass a typename as a parameter:

int X = FileRead(file, 9, char);

The concept is for FileRead(std::fstream, int pos, ???) to read pos*sizeof(whatever the type is) to get the desired position. I tried templates:

template<typename T>
T FileRead(std::fstream file, int pos, T type)
{
    T data;
    file.read(reinterpret_cast<char*>(&data), sizeof(data));
    return data;
}

but that required that I create a variable of the type to use every time I wanted to use FileRead, and I really don't feel like redesigning an entire program just because of one function, so is there anyway to use a typename as a parameter?

skaffman
  • 398,947
  • 96
  • 818
  • 769

4 Answers4

14

To use the name of a type as a parameter, use a template.

template<typename T>
T FileRead(std::fstream &file, int pos)
{
    T data;
    file.read(reinterpret_cast<char*>(&data), sizeof(T));
    return data;
}

This assumes that the type is default constructible. If it is not, I guess you would have difficulty streaming it out of a file anyway.

Call it like this:

char value=FileRead<char>(file, pos);

If you do not want to have to specify the type in the call, you could modify your API:

template<typename T>
void FileRead(std::fstream &file, int pos, T &data)
{
    file.read(reinterpret_cast<char*>(&data), sizeof(T));
}

Then call it like this - the type is inferred:

char value;
FileRead(file, pos, value);
1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
  • I think you'd need T as an argument rather than just as the return value. – Dan Olson Jun 01 '09 at 23:21
  • 2
    Call it like FileRead(file, pos); – sth Jun 01 '09 at 23:21
  • You will have to call it with the template argument. http://www.codersource.net/cpp_function_template_overloading.html – Daniel A. White Jun 01 '09 at 23:21
  • For readability you might want to add 'void' as the return type from FileRead(). – JSBձոգչ Jun 01 '09 at 23:28
  • Opps heh. Not just readability, if you leave it off then the type defaults to int – 1800 INFORMATION Jun 02 '09 at 00:05
  • 1
    I think in this case, types that are *not* default constructible couldn't be passed anyway (in c++03, at least), since for those, they are not PODs anyway and using read on them would be UB (and "const T" can't be modified either). If your compiler can do that as an extension and you would like to use non-default constructible types, you can use this definition instead: template T FileRead(std::fstream &file, int pos, T data = T()) { file.read(reinterpret_cast(&data), sizeof(T)); return data; }, which is the same technique also used by C::resize standard container functions. – Johannes Schaub - litb Jun 02 '09 at 12:54
4

Very simple:

template<typename T>
T FileRead(std::fstream file, int pos)
{
    T data;
    file.read(reinterpret_cast<char*>(&data), sizeof(data));
    return data;
}

and call it via:

char x = FileRead<char>(file, pos);
mmmmmmmm
  • 15,269
  • 2
  • 30
  • 55
0

I know this post is answered, but still, I had a problem with that and I found a simple way from this page answers. You can pass type name to any function like this:

template<typename T>
void function_name()
{
    //you can access your type here with T
    //you can create an object from T or anything you want
    //for example :

    T obj;

    //or:

    T obj=new T;

}

int main(){
    function_name<YourTypeName>();
}
Amin Rezaie
  • 31
  • 10
-3

There is no such things as types once your program is compiled. This is the style of C++.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445