I am implementing a print debugging system for a school assignment. I apologize if the answer is obvious, I am still learning c++.
I want to create a function, that takes three parameters: an error, a variable name, and the variable itself, and logs such information somewhere that can be viewed later. It would work like this:
int bad_variable = 2;
debug_log("Variable unexpected value", "bad_variable", bad_variable);
The problem is, I would have no idea what type of variable to expect when parsing bad_variable
to debug_log
, and therefore it cannot be defined. To my knowledge there is no way to parse a variable of an unknown type... however the c++ standard library does just that in the function std::to_string
!
std::to_string
could be the answer to my problem, I could just pass my bad_variable
into std::to_string
to convert it to a string, having debug_log
expect the output string. However, I would have to type std::to_string
every time I called the log function.
debug_log("Variable unexpected value", "bad_variable", std::to_string(bad_variable));
This to me seems like a patchwork solution to something that must have a simpler answer. I want to keep this debug function as simplistic as possible. How could I create a function that takes an unknown variable type, and generates a string containing the value of that variable?