sharkdp/dbg-macro

How to use this print std::pair or std::map<std::pair, int> ?

yinchunxiang opened this issue · 3 comments

Could someone help show me an example to print?
I do not find an example in the test. Thanks

You should just have to override the << operator as outlined in the README.md

#include "dbg.h"

template<typename T1, typename T2>
std::ostream& operator<<(std::ostream& out, const std::pair<T1, T2>& v) {
  out << "{" << v.first << "," << v.second << "}";
  return out;
}

int main() {

    dbg(std::pair<int, bool>{3, false});
    dbg(std::pair<bool, int>{true, 47});

}

And by defining the above dbg(std::map<std::pair<int, bool>, int>{{{1, true},2}, {{3, false}, 4}}); worked just fine for me.

Does this answer the question?

I think std::pair<…> is basic enough to add a custom pretty printer to dbg-macro. With the snippet by @DerekCresswell, it should be straightforward.

You should just have to override the << operator as outlined in the README.md

#include "dbg.h"

template<typename T1, typename T2>
std::ostream& operator<<(std::ostream& out, const std::pair<T1, T2>& v) {
  out << "{" << v.first << "," << v.second << "}";
  return out;
}

int main() {

    dbg(std::pair<int, bool>{3, false});
    dbg(std::pair<bool, int>{true, 47});

}

And by defining the above dbg(std::map<std::pair<int, bool>, int>{{{1, true},2}, {{3, false}, 4}}); worked just fine for me.

Does this answer the question?

Yes, thanks very much :)