Live data from Hacker News

Emulating Swift's “defer” in C, with Clang or GCC+Blocks

fdiv.net

31–33 of 33 posts

Re: Emulating Swift's “defer” in C, with Clang or GCC+Blocks

#31
post #24

Earlier quoted context omitted.

Not touching on the rest of what you said, but what's so bad about min? Apart from some internal clang stuff, like odd names and macros, it looks okay - I can see what the function is doing. // min template inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const _Tp& min(const _Tp& __a, const _Tp& __b, _Compare __comp) { return __comp(__b, __a) ? __b : __a; } template inline _LIBCPP_INLINE_VISIBILITY _LI…

It used to be #define min(x,y) ((x)

Better use:

    #define min(X, Y)                \
         ({ typeof (X) x_ = (X);          \
            typeof (Y) y_ = (Y);          \
            (x_ 

Re: Emulating Swift's “defer” in C, with Clang or GCC+Blocks

#32
post #27
post #22

C++ supports this sort of thing directly. It says something about C++ takeup that anyone would bother adding this kludge to C. Many people are scared of C++. The language has become incredibly complex. C++ templates are Turing-complete. You can do arbitrary computation at compile time. Once this was discovered, ever-fancier templates became a basic part of C++. (Take a look at the huge templates that implement "max"…

I think I just see it as different niches. C++ only adds to the code after a certain amount of complexity because there is so much magic its hard to manually translate to assembly in your head (at times). I certainly don't know much about c++ >03; the additions have been.... Probably not worth using over c for the massive probability and ability to contribute easily for small tools.

Edit:probability -> portability.

Re: Emulating Swift's “defer” in C, with Clang or GCC+Blocks

#33

Earlier quoted context omitted.

Can you explain more please?

Because a macro in C/C++ is just text expansion, the given implementation will evaluate its arguments more than once, which will not be obvious at the "call" site and could have unintended consequences if the expressions have side-effects.

Is there another way to do it as a macro that avoids this? How do you have temporary variables potentially in the middle of a larger expression, e.g. if (min(a,b) == 3) {...}
Post reply on HN