How would I create a function that can read values from a file into multiple numbers of variables varying from 1 to many?
Here's what I have so far, working from a similar question.
Note: I cannot use fold expressions (file >> ... >>> x
) because the code uses C 14.
test_stream_file Contents:
teststring_a teststring_b
Code:
template<typename... Args>
void fileread(std::fstream& file, Args...args)
{
using expander = int[];
expander{ (file >> (std::forward<Args>(args)), 0)... };
}
int main() {
std::fstream teststream;
teststream.open("test_stream_file", std::ios::in);
std::string a, b;
fileread(teststream, a, b);
std::cout << a << b;
}
When I run this I get error C2679: "binary '>>': no operator found which takes a right hand operator of type '_Ty' (or there is no acceptable conversion)". I'm a bit lost here. I read the documentation and another answer on std::forward but am still not seeing what is going wrong.
CodePudding user response:
This is the working code for you:
#include<iostream>
#include<sstream>
template<typename... Args>
void fileread(std::istream& file, Args&...args)
{
(file>> ...>>args);
}
int main() {
std::stringstream teststream("hallo welt");
//teststream.open("test_stream_file", std::ios::in);
std::string a, b;
fileread(teststream, a, b);
std::cout << a << b;
}
Why do you try to do perfect forwarding, you want to change the variables that are given to you function, so it should be regular references.
CodePudding user response:
You don't have to use std::forward
to forward args
since they must be lvalue references. This should be enough
template<typename... Args>
void fileread(std::istream& file, Args&...args)
{
using expander = int[];
expander{ ((file >> args), 0)... };
}