11

I read it from source code of opencv

#define IPP_FILTER_MEDIAN_BORDER(ippType, ippDataType, flavor) \
    do \
    { \
        if (ippiFilterMedianBorderGetBufferSize(dstRoiSize, maskSize, \
            ippDataType, CV_MAT_CN(type), &bufSize) >= 0) \
        { \
            Ipp8u * buffer = ippsMalloc_8u(bufSize); \
            IppStatus status = ippiFilterMedianBorder_##flavor(src.ptr<ippType>(), (int)src.step, \
                dst.ptr<ippType>(), (int)dst.step, dstRoiSize, maskSize, \
                ippBorderRepl, (ippType)0, buffer); \
            ippsFree(buffer); \
            if (status >= 0) \
            { \
                CV_IMPL_ADD(CV_IMPL_IPP); \
                return; \
            } \
        } \
        setIppErrorStatus(); \
    } \
    while ((void)0, 0)

I can understand while(0) in here, but why to add "(void)0".

no68
  • 143
  • 5
  • 5
    To include ASCII art `)0, 0)`? – ikegami Nov 03 '14 at 16:25
  • http://stackoverflow.com/questions/4178695/c-what-is-the-purpose-of-casting-to-void – blfuentes Nov 03 '14 at 16:26
  • possible duplicate of [do { ... } while (0) — what is it good for?](http://stackoverflow.com/questions/257418/do-while-0-what-is-it-good-for) – V-X Nov 03 '14 at 16:27
  • 2
    @V-X, I don't see the answer there -- this question isn't about the use of `do` -- and that question is marked as a duplicate of another. – ikegami Nov 03 '14 at 16:28
  • It doesn't matter which variant of the frequent question is it. I'm sorry that I marked the wrong one. – V-X Nov 03 '14 at 16:29
  • @V-X, I don't see the answer the other variant either – ikegami Nov 03 '14 at 16:30
  • http://stackoverflow.com/a/154138/2073232 – V-X Nov 03 '14 at 16:30
  • And the while ((void)0,0) is there to shut up some compilers and prevent stupid macro usage without partheneses. – V-X Nov 03 '14 at 16:31
  • 1
    @V-X, The first part of that is answer, that has already been posted as an answer, and that's not in the questions you linked. – ikegami Nov 03 '14 at 16:32

2 Answers2

16

At a guess, it's probably to shut up a compiler warning like "condition is constant".

Since (what C classifies as) a constant expression can't include a comma operator, using one can convince some compilers that an expression isn't a constant (even in a case like this, where it really is).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 2
    @Baldrickk: That explains why you use `do { ... } while (0)`, but *not* why you use `((void)0, 0)` as the condition. As this question explains, "I can understand while(0) in here, but why to add '(void)0'", so this is solely and specifically about the syntax of the condition, **NOT** the use of a `do { ... } while (0)` in general, which is *all* the other question/answers discuss. In short, it's not a duplicate, and you've completely missed the point of this question (and answer). – Jerry Coffin Nov 03 '14 at 18:25
  • Maybe you are right, but which compiler might do it? – no68 Nov 04 '14 at 09:08
  • 1
    @no68: That's almost impossible to guess. It's also possible that it was a lint program rather than a compiler *per se*. – Jerry Coffin Nov 04 '14 at 14:27
3

I am not 100% sure if that is the reason why, but I know for a fact that if you do

#define FOO do{ doStuff(); } while(0)

And then turn on the MSVC Compiler warning level 4 (/W4), you will get the warning C4127. But if you do:

#define FOO do{ doStuff(); } while((void)0,0)

the warning C4127 goes away. Maybe there are other reasons for using while((void)0,0), instead of while(0), I am not sure...

mchiasson
  • 2,452
  • 25
  • 27