fmt
fmt copied to clipboard
Annoying packet structs.
I often work with packet structures without alignment ( gcc attribute packet). fmt::format takes arguments by rvalue reference. Taking references of fields of such structures is reasonably forbidden by standard. Gcc gives me an errors like :
./common/pcap/pcap_file.cc:560:53: error: cannot bind packed field ‘pcaphdr.pcap_fileHdr::magic’ to ‘unsigned int&’
560 | LOG_DEBUG( "magic {}\n", pcaphdr.magic );
Obviously, I have to make a copy of such variable. I use template to reduce annoying coping :
template <class T>T inline stack_copy( T val){
return val;
}
LOG_DEBUG( "magic {}\n", stack_copy(pcaphdr.magic) );
Is my path right? Would it be reasonable to have such inside fmt? Have I another way?
PS LOG_DEBUG is simple macros around fmt::format.
You could use + as a workaround without introducing a new function:
LOG_DEBUG( "magic {}\n", +pcaphdr.magic );