sharkdp / dbg-macro

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

There is no way to output vector<uint8_t> such as vectors #136

Open heheda123123 opened 1 week ago

heheda123123 commented 1 week ago

if i use below code

#include <iostream>
#include <vector>
#include <iomanip>  // For std::hex and std::setw

std::ostream& operator<<(std::ostream& out, const std::vector<uint8_t>& v) {
    for (const auto& val : v) {
        // Output each value in hexadecimal format, padded with zeros to 2 digits
        out << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(val) << " ";
    }
    return out;
}

int main() {
  std::vector<uint8_t> aa {1,2,3};
  std::cout << aa;

  return 0;
}

output is

01 02 03

if I use dbg-macro, the output is


#include <iostream>
#include <vector>
#include <iomanip>  // For std::hex and std::setw
#include <dbg.h>

std::ostream& operator<<(std::ostream& out, const std::vector<uint8_t>& v) {
    for (const auto& val : v) {
        // Output each value in hexadecimal format, padded with zeros to 2 digits
        out << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(val) << " ";
    }
    return out;
}

int main() {
  std::vector<uint8_t> aa {1,2,3};
  dbg(aa);

  return 0;
}
[src\main.cpp:19 (main)] aa = {, , } (std::vector<uint8_t>)
heheda123123 commented 1 week ago

can use this method

  dbg(std::vector<uint32_t> (aa.begin(), aa.end()));
sharkdp commented 1 week ago

Thank you for reporting this. I'll actually reopen this. Would be great if we could support this out-of the box.