Live data from Hacker News

Detecting integer constant expressions in macros

lkml.org

11–20 of 25 posts

Re: Detecting integer constant expressions in macros

#11

"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?

I'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 th…

[deleted]

Re: Detecting integer constant expressions in macros

#13

"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?

I 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.

Re: Detecting integer constant expressions in macros

#14
I 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.

Re: Detecting integer constant expressions in macros

#17
post #8

Go on, tell me again about how C++ compile-time metaprogramming is so horrible ...

I 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.

Re: Detecting integer constant expressions in macros

#18
post #17
post #8

Go on, tell me again about how C++ compile-time metaprogramming is so horrible ...

I 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.

I think the point of that comment was "because clearly people still want to do these things and are thereby doing even worse in C".

Re: Detecting integer constant expressions in macros

#19
post #15

I've been writing C for 20 years and I have no idea what I am reading.

> ((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.

Hilariously 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.
Post reply on HN