6 ms·
Detecting integer constant expressions in macros
- deleted 9y ago[deleted]
- djhaskin987 9y agoThe reply of the guy Linus is addressing is worth a read: https://lkml.org/lkml/2018/3/20/871 https://lkml.org/lkml/2018/3/20/871
- chrisseldo 9y ago"I'm in awe of your truly marvelously disgusting hack. That is truly a work of art." Ha. How should one take such a comment?
- breadbox 9y agoMe, I'd put it on my resume.
- blazhennyjnerd 9y agoThe guy is a professor of bioinformatics in Göttingen. He probably won't be involved with such nonsense as resumes any time soon. https://www.uni-goettingen.de/de/uecker-martin-prof-dr-----cardiovascular-magnetic-resonance-imaging-research-group-uni-med/549182.html https://www.uni-goettingen.de/de/uecker-martin-prof-dr-----c...
- mikeash 9y agoIf I got such a reaction from someone like Linus, I’d probably print it out and frame it on my wall.
- Someone1234 9y agoI'd take it as a complement. Calling something a "disgusting hack," isn't inherently insulting, it is just a description of how well self documenting and obvious its workings are, which in this case is not at all. Whereas calling something a "work of art" particularly from Linus is high praise. And while there is some subjectivity to if something is a "hack" or not, the original author doesn't even seem to contend that too much. They were more interested in pointing out that this method likely works across compilers (since it relies on less undefined behaviour than it might first appear). In any case, it is a neat trick, that I wouldn't want to run across in a codebase since I'd have to spend too long figuring it out.
- deleted 9y ago[deleted]
- chrisseldo 9y agoI agree with you - I think I would take it as a compliment as well. Also, I take back my original post on what the best comment is in this post. This is better: "this will break the minds of everybody who ever sees that expression"
- AstralStorm 9y agoIt is like Duchamp's Fountain in a way. Challenges the perception and pushes what is possible. The trick can be properly documented in a page of text and wrapped in a macro... (D'oh!) It is probably non-conforming anyway or depends on a choice made by the implementation.
- vanni 9y agoIn my opinion it's clearly a compliment.
- kthejoker2 9y agoI think in this life at least all that's left for Linus to enjoy are marvelously disgusting hacks. So with that understanding this is probably the ultimate compliment.
- keldaris 9y agoI think it's the highest compliment I've ever seen Linus express to somebody. And he's not wrong, the hack is adorable. It would never have occurred to me that doing this was even possible in C.
- marshray 9y agoGo on, tell me again about how C++ compile-time metaprogramming is so horrible ...
- zaarn 9y agoI believe this is C, since we're on the LKML, not C++. Even if it's horrible, in a kernel you might want macros like these if a function call would sit in a hotpath and induce overhead.
- saurik 9y agoI think the point of that comment was "because clearly people still want to do these things and are thereby doing even worse in C".
- Doxin 9y agoJust because doing it in C is worse doesn't mean the way its done in C++ isn't bad.
- DoofusOfDeath 9y ago> Just because doing it in C is worse doesn't mean the way its done in C++ isn't bad. Maybe... I hesitate to call it "bad" unless there's a clearly better solution to the problem.
- ksherlock 9y agoI didn't believe it would work but it does. It boils down to sizeof(int) == sizeof(void) vs sizeof(int) == sizeof(int) As a non-standard extension, in gcc (and thus clang and icc for compatibility, it seems) void has a size of 1. In a conforming compiler, it's an error.
- AstralStorm 9y agoNot quite. Gcc it didn't fold it during preprocessing for a fully constant expression, which is allowed. In which case always sizeof(void) is a valid answer. Which is a compile error. That is at least what C99 draft seems to say. I'll check C11 draft next. Edit: same.
- kuon 9y agoI've been writing C for 20 years and I have no idea what I am reading.
- dilippkumar 9y ago> ((void)((x) 0l)) It took me a while to figure that 0l is 0 with lowercase "L" Whatever font that site uses makes it very very hard to distinguish l and 1.
- db48x 9y agoHilariously that site makes no choices at all about what font to use; it merely puts the message text into a pre tag, making it monospaced. You should choose a better font to display monospaced text in.
- kbob 9y agoI spent a long time wondering why it was multiplying by octal 1 as well. I'm not sure whether code like this should be critiqued for readability, but "0L" (uppercase) would have worked better.