Live data from Hacker News

Detecting integer constant expressions in macros

lkml.org

21–25 of 25 posts

Re: Detecting integer constant expressions in macros

#21

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

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

Re: Detecting integer constant expressions in macros

#22

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.

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

Re: Detecting integer constant expressions in macros

#23
post #9

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

Me, I'd put it on my resume.

The 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-----c...

Re: Detecting integer constant expressions in macros

#24
post #20
post #8

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

Just because doing it in C is worse doesn't mean the way its done in C++ isn't bad.

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

Re: Detecting integer constant expressions in macros

#25
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.

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