I want to achieve something like:
export_vars("path/to/file.dat", {"variable_name", obj}, {"another_variable", 2});
where obj can be any type as long as it has an << overload - the idea is to write to an ofstream later on. I have tried (for an initializer_list of pairs):
void
export_vars(const std::string& path, std::initializer_list<std::pair<std::string, std::any>> args)
{
for (auto& [name, var] : args)
std::cout << name << ": " << var << std::endl;
}
but std::any cannot be << without knowing the underlying type. Can it maybe be achieved using variadic templates and parameter pack expansion? I also tried something like:
template <class... Args>
void
export_vars(const std::string& path, Args... args)
{
(std::cout << ... << args.first << args.second) << std::endl;
}
but that's obviously wrong. Any suggestions?