Using std::stof in a macro

I am trying to convert data from a .dat file to a ttree in a ROOT macro, and need to check to see if some of the data are floats, and if so, convert them to such.
I have currently

			std::istringstream templine(line);
			std::vector<float> sub_lines;
			std::string temp1;
			std::cout << "might it be from the reuse of line?" << std::endl;
			while(getline(templine, temp1, ' ')){


where line is read in from the file, but on running using the .x command in root, I get an error that says “function stof(temp1.c_str()) is not defined in this scope”. I get the same error with or without the std:: and I am certian the data are reading in correctly

It seems std::stof is understood by ROOT:

root [1] cout << std::stof("1.5")  << endl;
root [2] 

Can you try this with your ROOT version ?

And an additional warning: you need to use the 2-argument version of stof to really ensure you have a float. stof will happily convert “1hello” to 1.0f (without throwing).

That is returning function not defined in this scope for me.

Which ROOT version? There should not be a problem with stof!

As (temporary) work-around, you could try:

root [0] #include <boost/lexical_cast.hpp> 
root [1] boost::lexical_cast<float>("2.5")
(float) 2.5f

I am using version 5.34/36 from April of 2016

You need ROOT 6 to have std::stof working

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.