I have the following template function:
template <typename N>
inline N findInText(std::string line, std::string keyword)
{
keyword += " ";
int a_pos = line.find(keyword);
if (a_pos != std::string::npos)
{
std::string actual = line.substr(a_pos,line.length());
N x;
std::istringstream (actual) >> x;
return x;
}
else return -1; // Note numbers read from line must be always < 1 and > 0
}
It seems like the line:
std::istringstream (actual) >> x;
Doesn't work. However the same function not templated:
int a_pos = line.find("alpha ");
if (a_pos != std::string::npos)
{
std::string actual = line.substr(a_pos,line.length());
int x;
std::istringstream (actual) >> x;
int alpha = x;
}
Works just fine. Is it an issue with std::istringstream and templates ???
I am looking for a way to read configuration files and load parameters which can be int or real.
EDIT solution:
template <typename N>
inline N findInText(std::string line, std::string keyword)
{
keyword += " ";
int a_pos = line.find(keyword);
int len = keyword.length();
if (a_pos != std::string::npos)
{
std::string actual = line.substr(len,line.length());
N x;
std::istringstream (actual) >> x ;
return x;
}
else return -1;
}
N? Do you get a compilation error?