I created a macro named DBG, which prints an expression itself and the value it evaluates to. So DBG(5+1)
should print 5+1 = 6
. That macro works fine.
Yet, if I encapsulate more than one of those macro, that becomes quite unreadible, because the "DBG" itself is always dragged around.
What I want to do is to remove all occurences of the substring "DBG" from the expression itself at compile time. So that DBG(DBG(5*3) + DBG(20/4))
the result would not be
5*3 = 15
20/4 = 5
DBG(5*3)+DBG(20/4) = 20
but instead
5*3 = 15
20/4 = 5
(5*3)+(20/4) = 20
If that's needed: The Macro looks like this: #define DBG(expression) debug_log((#expression, expression)
with debug_log being:
template<typename T>
inline constexpr T debug_log(const std::string_view& raw_expression, T&& x)
{
using namespace std;
cout << raw_expression << " = " << x << endl;
return x;
}
I wrote already a helper function that is supposed to do that, but I can't figure out how to concatenate two string_views at compile time.
inline constexpr auto clean_expression(const std::string_view& expression)
{
constexpr std::string_view macro_name = "DBG";
constexpr auto marco_name_length = macro_name.size();
auto pos = expression.find(macro_name);
if (pos == -1) {
return expression;
}
else {
auto after_macro_name = expression.substr(pos + marco_name_length);
auto length_before_macro = expression.size() - after_macro_name.size() - marco_name_length;
std::string_view string_before_macro_name = expression.substr(0, length_before_macro);
// TODO: Finish implementation by concatenating the string_before_macro_name and after_macro_name and cleaning the result
//auto partly_cleaned_string = concatenate(string_before_macro_name, after_macro_name)}; <-- that one is missing
//return clean_expression(partly_cleaned_string);
}
}