Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
yinchunxiang opened this issue Dec 28, 2019 · 3 comments · Fixed by #71
Closed

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

yinchunxiang opened this issue Dec 28, 2019 · 3 comments · Fixed by #71
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@yinchunxiang
Copy link

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

@DerekCresswell
Copy link
Contributor

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?

@sharkdp
Copy link
Owner

sharkdp commented Dec 28, 2019

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.

@sharkdp sharkdp added enhancement New feature or request good first issue Good for newcomers labels Dec 28, 2019
DerekCresswell added a commit to DerekCresswell/dbg-macro that referenced this issue Dec 29, 2019
DerekCresswell added a commit to DerekCresswell/dbg-macro that referenced this issue Dec 29, 2019
sharkdp pushed a commit to DerekCresswell/dbg-macro that referenced this issue Dec 29, 2019
sharkdp pushed a commit that referenced this issue Dec 29, 2019
@yinchunxiang
Copy link
Author

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants