|
Boost : |
From: Jason (jason_at_[hidden])
Date: 2004-07-16 04:03:29
after fidling around with template functions i discovered you could write lexical_cast without any template arguments _required_. Also I neat little C-Style cast although no one is probably interested in using that. Anyways the syntax is as followed for both:
// lexical cast improved:
string str = lexical_cast( 3.1415f );
int i = lexical_cast( "12345" );
// C-style cast fun!
string str = (cstyle_cast)3.1415f;
int i = (cstyle_cast)"12345";
I would say that anyone already using lexical_cast would'nt have a problem with the improved one. Let me know what you guys think and if we should implement that.
-Jason
SOURCE CODE:
#include <sstream>
using namespace std;
class cstyle_cast {
public:
template <typename Source> cstyle_cast( const Source & source ) {
ss << source;
}
template <typename Target> operator Target() {
Target target;
ss >> target;
return target;
}
private:
stringstream ss;
};
class lexical_cast {
public:
template <typename Source> lexical_cast( const Source & source ) {
ss << source;
}
template <typename Target> operator Target() {
Target target;
ss >> target;
return target;
}
private:
stringstream ss;
};
int main()
{
string str1 = (cstyle_cast)3.141592f;
string str2 = lexical_cast(3.141592f);
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk