So I have come across a problem where I have a function with multiple parameters. Out of usability I started overloading it so that for instace I can pass a std::string
instead of a std::fstream
instance. In this overload a new std::fsteam
will be constructed from that string and then the other function will be called. Like this:
void func(const std::string & filename) {
std::fstream file(filename);
func(file)
}
void func(std::fstream & file) {
// ...
}
This all works fine. But when you start doing this for more than one parameter or more than 2 possible types everything starts becoming a mess and you might have to write lots of overloads with duplicate code etc.
So I was wondering if there was a more elegant solution to this problem for as many parameters as you need.
I know this problem is not specific to C++ but I'm interested in solutions for the problem in C++.