sharkdp / dbg-macro

A dbg(…) macro for C++
MIT License
2.97k stars 257 forks source link

Nested dbg(...) expansion as not expected #121

Closed zhouzq-thu closed 1 year ago

zhouzq-thu commented 1 year ago
#include <vector>
#include "dbg.h"

// You can use "dbg(..)" in expressions:
int factorial(int n) {
  if (dbg(dbg(n) <= 1)) {
    return dbg(1);
  } else {
    return dbg(n * factorial(n - 1));
  }
}

int main() {
  std::string message = "hello";
  dbg(message);  // [example.cpp:15 (main)] message = "hello" (std::string)

  const int a = 2;
  const int b = dbg(3 * a) + 1;  // [example.cpp:18 (main)] 3 * a = 6 (int)

  std::vector<int> numbers{b, 13, 42};
  dbg(numbers);  // [example.cpp:21 (main)] numbers = {7, 13, 42} (std::vector<int>)

  dbg("this line is executed");  // [example.cpp:23 (main)] this line is executed

  factorial(4);

  return 0;
}

Screen Shot 2022-10-29 at 11 54 24

sharkdp commented 1 year ago

I noticed that too, recently. Unfortunately, I'm not really sure if that is something that can be fixed. If someone has ideas, please let me know.

That said, it's not really a bug. It should be kind of expected that dbg(some_macro(…)) would show the expanded version of some_macro.

zhouzq-thu commented 1 year ago

@sharkdp I just fix this by replacing "dbg:DebugOutput(...).print(...)", please check the code.