Code: Alles auswählen
std::istream& operator>> (std::istream& stream, glm::vec3& v)
{
stream >> v.x;
stream >> v.y;
stream >> v.z;
return stream;
}
boost::lexical_cast<vec3>("-6.929 5.7033 13.5175");
Code: Alles auswählen
std::istream& operator>> (std::istream& stream, glm::vec3& v)
{
stream >> v.x;
stream >> v.y;
stream >> v.z;
return stream;
}
boost::lexical_cast<vec3>("-6.929 5.7033 13.5175");
Code: Alles auswählen
namespace std
{
std::istream& operator>> (std::istream& stream, glm::vec3& v)
{
stream >> v.x;
auto a = stream.good();
stream >> v.y;
auto b = stream.good();
stream >> v.z;
auto c = stream.good();
return stream;
}
}
Code: Alles auswählen
#include <cstdio>
#include <sstream>
#include <boost/lexical_cast.hpp>
using namespace std;
namespace glm
{
struct vec3
{
float x, y, z;
};
}
namespace std
{
std::istream& operator>> (std::istream& stream, glm::vec3& v)
{
stream >> v.x;
stream >> v.y;
stream >> v.z;
return stream;
}
}
template <typename T>
T lexical_cast(const std::string& s)
{
std::stringstream ss(s);
T result;
if((ss >> result).fail() || !(ss >> std::ws).eof())
{
throw std::bad_cast();
}
return result;
}
int main()
{
stringstream a;
a << "3 5 12";
glm::vec3 v;
a >> v;
::lexical_cast<glm::vec3>("3 5 12");
boost::lexical_cast<glm::vec3>("3 5 12");
}
Ja im Grunde würde das aber wieder darauf hinauslaufen, dass ich mir einen Stringstream nehme, die Eingabe reinschreibe und dann die einzelnen Zahlen auslese. Also im Grunde würde ich damit wieder boost::lexical_cast nachbauen.MasterQ32 hat geschrieben:Blöde Frage, aber kann man nicht einfach für die boost::lexical_cast ne spezialisierte template-implementierung für die GLM-Sachen machen?
Müsste doch eigentlich möglich sein oder überseh ich da was?
Ja, jede Erklärung, was boost::lexical_cast macht läuft eigentlich auf den Code von Fall 2 hinaus. Wenn man sich dann aber die tatsächliche Implementierung anschaut, sieht man, dass doch noch eine ganze Menge mehr dahinter stecken muss. Was genau der Sinn von all dem ist, weiß ich nicht, aber ich vermute, wenn es von C++ Experten geschrieben ist, wird es schon irgendwo Sinn machen. Was natürlich nicht heißen muss, dass nicht trotzdem noch ein Bug drin steckt.Schrompf hat geschrieben:Klar kann man das machen. Aber es sollte auch so gehen. Das ist schon hinreichend seltsam. Ich sehe gar keinen Unterschied zwischen Version zwei und drei?
http://www.boost.org/doc/libs/1_57_0/doc/html/boost_lexical_cast/frequently_asked_questions.html hat geschrieben:Question: The implementation always resets the ios_base::skipws flag of an underlying stream object. It breaks my operator>> that works only in presence of this flag. Can you remove code that resets the flag?
Answer: May be in a future version. There is no requirement in Lexical Conversion Library Proposal for TR2, N1973 by Kevlin Henney and Beman Dawes to reset the flag but remember that Lexical Conversion Library Proposal for TR2, N1973 is not yet accepted by the committee. By the way, it's a great opportunity to make your operator>> more general. Read a good C++ book, study std::sentry and ios_state_saver.
http://stackoverflow.com/questions/10382884/c-using-classes-with-boostlexical-cast hat geschrieben:Your problem comes from the fact that boost::lexical_cast does not ignore whitespaces in the input (it unsets the skipws flag of the input stream).
The solution is to either set the flag yourself in your extraction operator, or just skip one character. Indeed, the extraction operator should mirror the insertion operator: since you explicitely put a space when outputting a Test instance, you should explicitely read the space when extracting an instance.