fmtlib / fmt

A modern formatting library
https://fmt.dev
Other
19.9k stars 2.43k forks source link

can't print volatile void* #4049

Closed ZXShady closed 5 days ago

ZXShady commented 1 week ago

this is the same issue with std::ostream operator that will be fixed in C++23

[godbolt](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,selection:(endColumn:2,endLineNumber:5,positionColumn:2,positionLineNumber:5,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:'%23include+%3Cfmt/core.h%3E%0A%0Aint+main()+%7B%0A++fmt::print(%22%7B%7D%22,reinterpret_cast%3Cconst+volatile+void*%3E(0))%3B%0A%7D'),l:'5',n:'1',o:'C%2B%2B+source+%231',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:g112,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,libs:!((name:fmt,ver:trunk)),options:'-O2',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+gcc+11.2+(Editor+%231)',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4)

Dani-Hub commented 1 week ago

What the submitter is referring to is the adopted paper P1147.

vitaut commented 1 week ago

Since {fmt} disallows arbitrary pointers you still need a cast in which case volatile can be omitted. Is there a more compelling example?

Dani-Hub commented 1 week ago

Since {fmt} disallows arbitrary pointers you still need a cast in which case volatile can be omitted. Is there a more compelling example?

I think a more compelling example is that you have actually a volatile T* p for formatting, but it doesn't suffice to add apply static_cast<const void*> to p in your code, but you also need to add another const_cast<const T*> to it first.

ZXShady commented 1 week ago

@vitaut

lets say I have a volatile int* and want to format it I have to do this

volatile int* p = (int*)0xdeadbeef;
fmt::format("{}",const_cast<void*>(static_cast<volatile void*>(p))); // or do static_cast<void*>(const_cast<int*>(p))

if fmt allowed to print const volatile void* then I do not need the last const cast.

the godbolt link was to show that you can't print it

vitaut commented 1 week ago

Makes sense, a PR to avoid volatile void* support would be welcome.