Function-in-a-line!!! (C/C++)

Since I have started vision processing, these are nifty functions, that I’ve consicified into a single line so they are out of the way of your real code!

//string funcs, requiring string and sstream
string bool2str(bool in) { stringstream str; str << in; return str.str(); }

string int2str(int inp) { stringstream str; str << inp; return str.str(); }

string double2str(double in) { stringstream str; str << in; return str.str(); }

//math functions!
double tanDeg(double input) { return tan((input) * pi / 180); }
double hypotenuse(double legA, double legB) { return sqrt(((legA*legA) + (legB*legB))); }
double leg(double hypotenuse, double leg) { return sqrt(((hypotenuse*hypotenuse) - (leg*leg))); }

Give me any feedback and post more of these single-liner c functions!

There is a <cmath> function for computing the length of the hypotenuse.

For doing numeric type to string conversions, C++11 supports std::to_string, but without C++11 you could still do your stringstream method with a templated function (since the function body is exactly the same regardless of the argument type).

Use a template:

template<typename T>
string as_string(T const& t){
        stringstream ss;
        return ss.str();

Templates are the best way to go there. C++11 reportedly has a to_string function in namespace std but it is absent from GCC 4.8.2. Kind of irrelevant seeing as WindRiver uses GCC 3.4.4…

Alex Brinister