I need to be able to pass a typename as a parameter:
我需要能够将typename作为参数传递:
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:
这个概念是为FileRead(std :: fstream,int pos,???)读取pos * sizeof(无论类型是什么)来获得所需的位置。我试过模板:
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?
但这需要我创建一个每次我想使用FileRead时使用的类型的变量,我真的不想因为一个函数而重新设计整个程序,所以无论如何都要使用typename作为参数?
3 个解决方案
#1
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:
如果您不想在调用中指定类型,则可以修改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);
#2
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);
#3
There is no such things as types once your program is compiled. This is the style of C++.
编译程序后,没有类型的东西。这是C ++的风格。
#1
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:
如果您不想在调用中指定类型,则可以修改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);
#2
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);
#3
There is no such things as types once your program is compiled. This is the style of C++.
编译程序后,没有类型的东西。这是C ++的风格。