I've written a series of function templates to convert arbitrary stuff to text as painlessly as possible. For example,
print(std::pair<int, int> {13, 1});
will print {13, 1}
and something longer like
std::vector<std::tuple<double, std::string>> vect;
for(int i=0;i<3;++i) {
double root = sqrt(i);
vect.push_back( {root, "sqrt " + std::to_string(i) } );
}
print(vect);
Will output: { {0, "sqrt 0" }, {1, "sqrt 1"}, {1.41421, "sqrt 2"} }
Let's say I have the following struct:
struct point { int x, y; };
How dangerous is it to write something like the following code?
std::vector<point> my_points;
//Add points into my_points;
print(reinterpret_cast<const std::vector<std::pair<int, int>>&>(my_points));
It compiles in gcc and it produces the expected output, although I'm concerned it could fail if someone were to try porting the code.